I have a linq query that needs grouping on an id to count rows of 2 status values, then I would like to call a method that performs a calculation on the resulting counts. Perhaps someone can help me understand a better way to do this. When I run this I see an exception that the ‘calcRate’ is not recognized by linq, but I’ve done something similar before and it works fine. I may be approaching this incorrectly as well:
var query = from c in (context.table
.Where(s => s.date >= startDate && s.date <= endDate)
.GroupBy(s => new { s.id })
.Select(s => new
{
id = h.Key.id,
rate = calcRate(h.Count(s=>s.status == 1), h.Count(s=>s.status != 3))
}).ToList())
select new { id = c.id, rate = c.rate };
You should do the part that the DB can do in the DB and then use
.AsEnumerable()to bring the results back into .NET to finish the calculations.