0

When I do

var arr = new int[] { 100, 200, 300, 400 };
var q = arr.AsQueryable();

q is an IQueryable<int> (concrete an EnumerableQuery<int>). Now, when I call

Queryable.Sum(q)

where can I find the sum-Logic? When I look into the Sum-Method, I see that there will the Expression-Tree executed with the Method Queryable.Sum. But where can I find the Iteration over the array which will sum the values?

I understand that AsQueryable will create the Expression of the IEnumerable and Queryable.Sum() executes it with the EnumerableQuery-Provider but where is the effective Logic which will be executed?

BennoDual
  • 5,865
  • 15
  • 67
  • 153
  • 4
    The query is executed by the underlying `QueryProvider`. In that case I'd guess that there is a query provider that simply parses the expression tree and calls the appropriate `IEnumerable` extensions. But I did not look it up in the sources. – René Vogt May 30 '20 at 15:16
  • I thought so too - but can't find the job. I also inserted a breakpoint in Enumerable.Sum () - but was not called. – BennoDual May 30 '20 at 15:20
  • 2
    You can start in the reference source [here](https://referencesource.microsoft.com/#System.Core/System/Linq/SequenceQuery.cs,4c59e0377c62ac70) and scroll down to the `Execute` method and search on from there following the `EnumerableExecuter` – René Vogt May 30 '20 at 15:21
  • .net core source for .`Enumerable.Sum()` is here: https://github.com/dotnet/runtime/blob/master/src/libraries/System.Linq/src/System/Linq/Sum.cs. You could configure Visual Studio to step into the framework source but I can't remember how at the moment. – teambanana May 30 '20 at 17:39
  • @RenéVogt I have found it now. There will be a MethodCallExpression created. With EnumerableRewriter this is then translated from Queryable.Sum() to Enumerable.Sum() – BennoDual Jun 02 '20 at 07:22

0 Answers0