Simplify with Linq

Takes simple tasks and makes them so brutally clean.  Simple examples…

int [] i = { 1, 3, 5, 6,7, 9 };
int  [] i2 = { 2, 4, 5, 8, 10, 3, 5 }; 

 // Combination of all (repeats included).
IEnumerable<int> concat = i.Concat(i2).ToList();

// Only those ints from i not in i2.
IEnumerable<int> except = i.Except(i2).ToList(); 
  // Only the unique ints found in i and i2, and order them.
IEnumerable<int> union = i.Union(i2).OrderBy(r => r).ToList();
  // Only those ints that exist in both i and i2.
IEnumerable<int> intersect = i.Intersect(i2).ToList(); 

2 thoughts on “Simplify with Linq

    • The future is very bright for Linq. Check it out. Currently there are 3 “flavors” baked into .net (to objects, to sql, to xml). The Linq-to-sql, while very handy, is “on the way out” as Enterprise Framework takes over that slot, but the other two are fantastic editions to a developer’s toolbelt (especially linq to objects).

Leave a reply to harperj1029 Cancel reply