EF Core: How can i Select (Distinct) after join?

entity-framework-core

Question

Basically i want this as query:

SELECT DISTINCT c.description FROM students s
join courses c on s.courseId = c.Id
WHERE c.Id = 100

How do i do this in EF Core? When i do :

db.Students
.Include(s => s.courseId)
.Select( -- how can i select for course description? --)
.Distinct()

Bear with me. I am new to Entity Framework.

1
0
5/29/2018 5:18:44 PM

Accepted Answer

First of all, your Include expression handles the joining for you, so you don't have to specify how your going to join the table. So your include will look like this:

_db.Students.Include(s => s.Course)

Then you'd accomplish your select through the use of a lamba expression like this:

_db.Students .Include(s => s.Course).Select(s => s.Course.Description).Distinct()
1
3/15/2018 11:58:16 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow