Where with conditional lambda

zamu

New member
Joined
Oct 21, 2021
Messages
1
Programming Experience
5-10
Hi everyone,

I'm struggling to understand this lines of code but I don't grow out of it.

The instruction is the following

C#:
var targetHandles = target.Where(t => true);

where target is an ObservableCollection<T> and T is DtoBase

Thanks in advance for your answers

Ric
 
The lambda with the Where() is supposed to return true or false for each item. If true is returned by the lambda, then the item will be part of the returned enumerable. In the code above, it is always returning true. So it is basically saying take all items. The code should have just been written as:
C#:
var targetHandles = target;
and not bother with the overhead of scanning through the items to determine whether to accept the item or not.
 
Since filter is true for all items the only change would ObservableCollection<T> as IEnumerable<T> iterator. The base class of former also implements the latter and could be iterated/cast directly.
 
Back
Top Bottom