The set operator operates on the set or sequence set of elements and returns a set. There are four collection query operators in LINQ: distinct, union, intersect, and except T.
1. Distinct
The distinct operator deletes duplicate values in a set and returns distinct elements in the set.
1>. Prototype Definition
public static IQueryable<TSource> Distinct<TSource>(this IQueryable<TSource> source);
public static IQueryable<TSource> Distinct<TSource>(this IQueryable<TSource> source, IEqualityComparer<TSource> comparer);
2>. Example
int[] fibonacci = new int[] { 1, 1, 2, 3, 5, 8, 13, 21 };var expr = from f in fibonacci select f;expr.Distinct();
int[] fibonacci = new int[] { 1, 1, 2, 3, 5, 8, 13, 21 };var expr = from f in fibonacci where f > 5 select f;expr.Distinct();
var expr = from p in context.Products select p.ProductName;expr.Distinct();
var expr = context.Products .Select(p => p.ProductName) .Distinct();
2. Union
The Union operator returns each element in two sequences or collections. Unlike the Concat operator, the Union operator returns distinct elements, while the Concat operator returns all values.
LINQ series: LINQ to object set Operators