So basically i have this method.
public List FilterCustomersByStatus(List source, string status)
{
return (List
does the Enumerable.Where create a new instance of
WhereListIterator
Yes.
and if so why would anyone do this
Because it allows lazy streaming behavior. Where
won't have to filter all the list if its consumer wants only first or second entry. This is normal for LINQ.
because thats an unnecessary loss of performance and functionality since i always have to create a new list (.ToList())
That "loss of performance and functionality" comes from your design. You don't need List
after filtering, because it's pointless to do any modifications on it.
Update: "why is it implemented so"
Because it it implemented over IEnumerable
, not IList
. And thus it looks like IEnumerable
, it quacks like IEnumerable
.
Besides, it's just so much easier to implement it this way. Imagine for a moment that you have to write Where
over IList
. Which has to return IList
. What should it do? Return a proxy over original list? You'll suffer huge performance penalties on every access. Return new list with filtered items? It'll be the same as doing Where().ToList()
. Return original list but with all non-matching items deleted? That's what RemoveAll
is for, why make another method.
And remember, LINQ tries to play functional, and tries to treat objects as immutables.