Can I split an IEnumerable into two by a boolean criteria without two queries?

Posted by SFun28 on Stack Overflow See other posts from Stack Overflow or by SFun28
Published on 2010-12-28T20:39:47Z Indexed on 2010/12/29 1:54 UTC
Read the original article Hit count: 520

Filed under:
|

Folks,

Is it possible to split an IEnumerable into two IEnumerables using LINQ and only a single query/linq statement? In this way, I would avoid iterating through the IEnumerable twice. For example, is it possible to combine the last two statements below so allValues is only traversed once?

IEnumerable<MyObj> allValues = ...
List<MyObj> trues = allValues.Where( val => val.SomeProp ).ToList();
List<MyObj> falses = allValues.Where( val => !val.SomeProp ).ToList();

© Stack Overflow or respective owner

Related posts about .NET

Related posts about LINQ