motivationofyours
motivationofyours

Reputation: 55

Entity Framework core: sum of sum doesn't work

I'm trying to use this query using EF core:

entity.OrderBy(e => e.FieldOne.Sum(fo => fo.NestedField.Sum(nf => nf.SecondNestedField)));

Error:

Cannot perform an aggregate function on an expression containing an aggregate or a subquery. Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

Is it possible to rewrite it EF core way?

Upvotes: 1

Views: 197

Answers (1)

vernou
vernou

Reputation: 7590

It's sound not possible to call a complex expression in Sum. But you can flat the list and do a simple Sum like :

entity.OrderBy(e.FieldOne.SelectMany(fo => fo.NestedField).Sum(nf => nf.SecondNestedField));

Upvotes: 2

Related Questions