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.
Up vote 1 down vote favorite 1 share g+ share fb share tw.
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 g, Func 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 c# linq-to-entities entity-framework-4.1 link|improve this question edited May 27 '11 at 14:34 asked May 27 '11 at 14:17Andrej Slivko35518 62% accept rate.
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.
I'm using code only approach – Andrej Slivko May 27 '11 at 14:37 @qrow: Then you cannot reuse your method. – Ladislav Mrnka May 27 '11 at 14:41.
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"), ... }.
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.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.