Find all answers

I have an IEnumerable<T> that I would like to filter based on the LINQ predicate. I tried using Where on IEnumerable , as usual, but this time I came across something interesting. When calling Where on IEnumerable , with a predicate, I get an empty list in response. I know that he should create a list with two elements in it. If I use FindAll , with the same predicate, it then produces the correct result.

Can someone explain to me why this is happening? I always thought Where is a lazy version of FindAll , which also returned IEnumerable instead of List . Must be more than that? (I did some research, but to no avail).

the code:

 IEnumerable<View> views = currentProject.Views.Where( v => v.Entries.Any(e => e.Type == InputType.IMAGE || e.Type == InputType.VIDEO)); IEnumerable<View> views = currentProject.Views.FindAll( v => v.Entries.Any(e => e.Type == InputType.IMAGE || e.Type == InputType.VIDEO)); 
+7
list c # linq ienumerable
source share
3 answers

You can find your answer here: LINQ, Where () and FindAll () . Basically, if you call .ToList () in its "place", they will be the same.

You can learn more about the differences between deferred and immediate execution: https://code.msdn.microsoft.com/LINQ-Query-Execution-ce0d3b95

+1
source share

My best guess would be that something happens between a Where call that creates an enumerator and a place in your code where the results are actually used (i.e. where MoveNext and (get_) Current of this enumerator are actually called, for example, from ToList).

+2
source share

Yes, where is the lazy version of findall. FindAll () is a function of type List, it is not a LINQ extension method, such as Where. The FindAll method in List, which is an instance method that returns a new List with the same element type. FindAll can only be used on List instances, while LINQ extension methods work on any type that implements IEnumerable.

The main difference (in addition to being implemented on: IEnumerable vs. List) is that Where implements deferred execution, where it actually doesn't search until you need it (using it in a foreach loop, for example ) FindAll is an immediate execution method.

I will refer to a data structure called an expression tree to understand deferred execution, you only need to understand that the expression tree is a data structure, such as a list or a queue. It contains a LINQ to SQL query, not the query results, but the actual elements of the query itself.

To understand the Where Work, we need to see that if we write code

 var query = from customer in db.Customers where customer.City == "Paris" select customer; 

The request is not executed here, while it is executed in the foreach loop

To understand LINQ and delayed execution

+1
source share

All Articles