split a list using linq

Posted by Anonym on Stack Overflow See other posts from Stack Overflow or by Anonym
Published on 2011-01-06T01:01:40Z Indexed on 2011/01/06 1:53 UTC
Read the original article Hit count: 529

Filed under:

I've the following code:

  var e = someList.GetEnumerator();
  var a = new List<Foo>();
  var b = new List<Foo>();
  while(e.MoveNext())  {
     if(CheckCondition(e.Current)) {
         b.Add(e.Current);
         break;
     }
     a.Add(e.Current);
 }

while(e.MoveNext())
  b.Add(e.Current)

This looks ugly. Basically, iterate through a list and add elements to one list until some condition kicks in, and add the rest to another list.

Is there a better way e.g. using linq ? CheckCondition() is expensive, and the lists can be huge so I'd prefer to not do anything that iterates the lists twice.

© Stack Overflow or respective owner

Related posts about c#