Sad, Sad Day


Today was a sad day, only because I couldn’t find an efficient way to use LINQ in my scenario.  I am presented a list which I need to split into two based on simple criteria.  Selecting a sublist is easy enough, selecting the rest is not.  

Sadly, below is the code excerpt I was left with:

/*
You don't understand. I coulda had class. I coulda been a contender. I coulda been somebody, instead of a bum, which is what I am, let's face it. It was you, Linq.
this.AccountsUnderContract = value.Where(acct => acct.AccountHolder.Equals(this.Name1)).ToList();
this.AccountsOccupied = value.Except(this.AccountsUnderContract).ToList();
*/
this.AccountsUnderContract = new List(value.Count);
this.AccountsOccupied = new List(value.Count);
foreach (var acct in value)
{
   if (acct.AccountHolder.Equals(this.Name1))
   {
      this.AccountsUnderContract.Add(acct);
   }
   else
   {
      this.AccountsOccupied.Add(acct);
   }
}

Obviously the middle-ground is two where clauses, but that’s still minimally twice the number of iterations. Does anyone have a performant way around this?

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s