I have this query that I am using to get students, I also want to include the current course in which student is studying. How to change this query to make it work?
var students = Context.Students
.Where(x=>x.Race == "SomeRace")
.Include(x=> x.StudentCourse.FirstOrDefault(x=>x.Status == CourseStatus.Current))
This query will through an error, since EF does not allow to put where condition in the Include. Any turn around? Any other way to write this query?
Update: If I use the solution proposed by @CoryNelson which is something like this:
studentResult = Context.Students
.Where(x=>x.Race == "SomeRace")
.Select(x=>new
{
Student = x,
CurrentCourse = x.StudentCourse
.FirstOrDefault(y=>y.Status == CourseStatus.Current)
};
The studentResult I get is not a list, like if I try to do
foreach( student in studentResult)
{
finalResult.Add(
new StudentDto
{
FirstName = student.Firstname // I do not get student first name, intellisense is not letting me select FirstName, which means studentResult is not a collection?
}
}