I have query:
ctx.PrintJobs
.GroupBy(pj => pj.Department.Name)
.Select(g => new PrintJobReportItem
{
A3SizePercentage = g.Sum(p => p.DocumentSize == "a3" ? p.TotalPhisicalPages : 0d) / g.Sum(p => p.TotalPhisicalPages) * 100,
...
}
and it works.
I have alot of those Perecentage values to count, so i tried to refactor this code to:
ctx.PrintJobs
.GroupBy(pj => pj.Department.Name)
.Select(g => new PrintJobReportItem
{
A3SizePercentage = g.PercentageOf(p => p.DocumentSize == "a3"),
...
}
...
public static class GroupingEx
{
public static double PercentageOf(this IGrouping<string, PrintJobItem> g, Func<PrintJobItem, bool> trueCondition)
{
return g.Sum(p => trueCondition(p) ? p.TotalPhisicalPages : 0d) / g.Sum(p => p.TotalPhisicalPages) * 100;
}
}
But then i'm getting error:
System.NotSupportedException : LINQ to Entities does not recognize the method 'Double PercentageOf...
I understand why i'm getting this error. Is there other ways to extract method group supported by Linq 2 Entity into single method? or am i stack with copy pasting same code bits? Linq 2 Object is not an option