I have a linq-to-sql query that I use to fill an object that has ints as properties and whose function is to hold counts.
The query looks like this (for now)
var TheQuery = from.....
where ....
in thefilteredresults
select new MyModel()
{
Total = thefilteredresults.Count(),
TotalTime = (from x in thefilteredresults
where x.Outcome == 4).Sum(t => t)
}.Single();
I’m have some problem filtering first and then counting based on the filtered results of the initial where clauses.
What do I need to modify?
Thanks.
You can hack this together:
SQL Server will optimize out the unneeded grouping. It is probably best do document why you wrote it like this.
Edit: I included a strange-looking cast to int?. The reason for this is to tell Linq to SQL to assume that the value is nullable and to use a COALELCE function call to convert the NULL to 0. This is a hack that should be documented, too.