LINQ Distinct Query Operator

By Peter Bromberg

Returns distinct elements from a sequence. Distinct<TSource>(IEnumerable<TSource>) -- Returns distinct elements from a sequence by using the default equality comparer to compare values. Distinct<TSource>(IEnumerable<TSource>, IEqualityComparer<TSource>) -- Returns distinct elements from a sequence by using a specified IEqualityComparer<T> to compare values.

public static void DistinctExample()
{
    string[] names = new string[] { "Peter", "Paul", "Mary",
        "Peter", "Paul", "Mary", "Janet" };

    var q = (from s in names
            select s).Distinct();

    foreach (var name in q) {
       Console.WriteLine(name);
    }

   Console.ReadLine();
}

LINQ Distinct Query Operator  (1164 Views)