-2
string[] fruits = { "apple", "passionfruit", "banana", "mango",
                  "orange", "blueberry", "grape", "strawberry" };
List<int> lengths = fruits.Select(fruit => fruit.Length).ToList();

Enumerable.ToList() or Enumerable.ToArray() performs eager evaluation, they force the evaluation. Does Enumerable.AsEnumerable() do the same ?

Thanks

Cleptus
  • 3,446
  • 4
  • 28
  • 34
Observer
  • 122
  • 6
  • 1
    Are you talking about Xamarin's `ToEnumerable` or .NET's `AsEnumerable` ("To" or "As")? You say one in the title and another in the question – ShamPooSham Nov 19 '20 at 13:47
  • AsEnumerable doesn't do anything. ToEnumerable I'm not sure – ShamPooSham Nov 19 '20 at 13:47
  • @ShamPooSham I just edited the question, sorry and thanks for the answer – Observer Nov 19 '20 at 13:52
  • 1
    Msdn has an article [Classification of Standard Query Operators by Manner of Execution (C#)](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/classification-of-standard-query-operators-by-manner-of-execution). it Has a nice table with every method and it execution type – Drag and Drop Nov 19 '20 at 13:57

1 Answers1

1

In short, no. It does absolute nothing at all, it just return the input collection, typed with a different interface.

It's only practical purpose it to switch from IQueryable<T> to IEnumerable<T>, so that further LINQ methods are performed with the enumerable version instead of the queryable, making evaluation to go to "client-side".

But, in itself, it doesn't do anything.

Alejandro
  • 7,290
  • 4
  • 34
  • 59
  • 1
    You are right but making evaluation client-side is not exactly nothing : it can involve transfering a huge amount of data from the DB to the client. That's why this conversion is no more automatic on the latest EF Versions. – XavierAM Nov 19 '20 at 13:55
  • @XavierAM By itself it does nothing. It only changes the compile-time type of the variable holding it, which in turn causes further calls to chose a different overload. Using a cast or the `as` keyword does have the very same effect too. – Alejandro Nov 19 '20 at 13:59