Set Operators
Using .Select()
method returns a duplicate items. We use .Distinct()
to differentiate with others.
birds.Select(b => b.Color);
// return "Red", "Black", "Red", ..
birds.Select(b => b.Color).Distinct();
// return "Red", "Black", ..
We could find items that are not in the collection. Like we want to find colors in one collection differ than bird colors.
var colors = new List<string> {"Pink", "Blue", "Black"};
colors.Except(birds.Select(b => b.Color).Distinct());
// return "Pink", "Blue"
Next is Union()
operator. It takes both items in 2 collections and remove the duplicates.
colors.Union(birds.Select(b => b.Color));
// return "Pink", "Blue", "Black", "Red", "White", "Black"
Next is Intersect()
operator. It only returns items that are exist in both collection.
colors.Intersect(birds.Select(b => b.Color).Distinct());
// return "Black"