I have an IEnumerable<T>
that I wanted to filter based on a LINQ predicate. I tried using Where
on the IEnumerable
as I normally do, but this time I stumbled upon something interesting. When calling Where on the IEnumerable
, with the predicate, i get an empty list in return. I know it has to produce a list with two items in it. If I instead use FindAll
, with the same predicate, it then produces the correct result.
Can anyone explain to me, why this is happening? I always thought that Where
was kind of a lazy version of FindAll
, that also returned an IEnumerable
instead of a List
. There must be more to it than that? (I have done some research, but to no avail.)
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));