Sometimes you want to use a Difference (or Minus) set operation which goes like this: Give me all elements from set A that do not exist in set B. Or simply put A – B. The LINQ Except method does this if both sets A and B are of the same type, but frequently the sets are of different types and must have their identifiers matched. Here is a quick little example of how to do this with LINQ. Wolfram MathWorld shows the operation’s definition as:
static void Main(string[] args)
{
List<string> names = new List<string>
{ "Noah", "Sarah", "Josiah", "Craig", "Carolin" };
Dictionary<string, string> visiting = new Dictionary<string, string>()
{ { "Noah", "Turkey" }, { "Craig", "Germany" }, { "Sue", "Bangalore" } };
var minus =
from n in names
let places = from p in visiting select p.Key
where !places.Contains(n)
select n;
foreach (var v in minus)
Console.WriteLine(v);
Console.ReadKey();
}
Output:
Sarah Josiah Carolin
The dot notation way of doing this would be "names.Where(a => !visiting.Select(b => b.Key).Contains(a))". I realize this all seems to be relatively trivial for one familiar with LINQ, but at the time of writing this there isn’t a decent topic popping up on Bing or Google when searching for LINQ Minus C# with two types of sets, now hopefully there will be. If you know of a more efficient way, please by all means post a comment, thanks.
References