I have the following two table:
-Groups-
Id
Name
-Members-
Id
GroupId (Group.Id is related to Member.GroupId)
Name
IsActive (bit)
How can I writing a LINQ query that will sort by the number of IsActive members highest to lowest in a group?
The query would look something like this
//pseudo code
from grp in database.Groups
orderby Count(grp.Members.where(m=>m.IsActive == true)) descending
select grp
You can use let clause for this.
As a result you will get following:
Another way to achive desired ordering is to use
select ... into. Queries will be pretty similar, but you should be aware of differences between this two approaches: Is linq’s let keyword better than its into keyword?