1

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

Andrej Slivko
  • 1,236
  • 2
  • 12
  • 27

2 Answers2

1

It is possible in ESQL (inline function copied to multiple ESQL queries - example) and it is possible with Linq-to-entities if you are using EDMX where you can define model defined function. As I know code first (= no EDMX) doesn't support that - in such case you must use the approach mentioned by @Daniel where you materialize the result and use linq-to-objects to execute your method.

Community
  • 1
  • 1
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
0

You can first fetch the data from the database and then use your method on the local data:

ctx.PrintJobs.GroupBy(pj => pj.Department.Name)
             .ToList()
             .Select(g => new PrintJobReportItem
                          {
                              A3SizePercentage = 
                                  g.PercentageOf(p => p.DocumentSize == "a3"),
                              ...
                          }
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
  • I know that Linq to Object can handle that, but i don't want to fetch data, i would like to use Linq to Entity. I don't need results right away, i need to return IQueryable from this function, so that other functions could also add other things that would translate to sql – Andrej Slivko May 27 '11 at 14:33