Using Multiple Group Functions in IQueryable <T>
Given the following simple object
public class Foo {
public int PrimaryKey;
public int ForeignKey;
public bool FlagOne;
public bool FlagTwo;
}
Suppose I got it IQueryable<Foo>. Typically, if I want to perform a count operation for each flag, I would do the following:
IQueryable<Foo> foos = GetFoos();
var total = foos.Count();
var flagOneTotal = foos.Count(p => p.FlagOne);
var flagTwoTotal = foos.Count(p => p.FlagTwo);
In EF, the above would execute 3 queries in the database. I would like to get all this in one request.
For grouping, I can do this to execute a single request:
var q = from foo in foos
group foo by foo.ForeignKey into g
select new {
ForeignKey = g.Key,
Total = g.Count(),
FlagOneTotal = g.Count(p => p.FlagOne),
FlagTwoTotal = g.Count(p => p.FlagTwo)
};
var list = q.ToList();
But how would I do the same if I want to get the totals for all the elements, regardless of the foreign key in one request, and one anonymous object?
In other words, as I would say .net, that all elements in foosshould be considered by 1 group, so I can perform Count operations on them.
+4
user1718673