I have a repository accessing my Entity Framework. I have a method that looks like this:
public TEntity FindOne(Expression<Func<TEntity, bool>> criteria)
{
var query = _queryAll.Where(criteria);
return query.FirstOrDefault();
}
I have 2 Entities that have a one to many relation. Lets call them Courses
and Students
. A Course
can have multiple Students
. I'd like to write a query that returns the Course that has the most students.
Courses.OrderByDescending(x=>x.Students.Count()).FirstOrDefault();
But how would I write that as a Func<T, bool>
?
I hope it's not
(x=>x.OrderBy(y=>y.Students.Count()).FirstOrDefault().id == x.id)
Because adding another criteria looks like it wouldn't work:
(x=>x.OrderBy(y=>y.Students.Count())
.FirstOrDefault().id == x.id
&& x.CourseName == "CS101")