{TOC}
| Namespace: | System.Linq |
| Assembly: | System.Core.dll |
| Extends: | IEnumerable<T> |
Back to
Standard Query Operator IndexEditIntroduction
The Distinct operator eliminates duplicate elements from a sequence.
EditMethod Signatures
// 1 - Returns each element, once at most if it is a duplicate.
public static IEnumerable<TSource> Distinct<TSource>(
this IEnumerable<TSource> source)
// New in January CTP
// 2 - Returns each element, once at most if it is a duplicate.
// A custom function to determine equality is used.
public static IEnumerable<TSource> Distinct<TSource>(
this IEnumerable<TSource> source,
IEqualityComparer<TSource> comparer)EditExceptions
Throws an ArgumentNullException if
source is null
EditPseudo-code
Overload 1
If
source is null, throw an ArgumentNullException.
Iterate the
source sequence.
Check to see if this element value has already been returned (Uses EqualityComparer<TSource>.Default)
If this element value hasn't been returned, Add it to the list of those that has and Return the current element (pick up from here when the next element is requested).
Overload 2
If
source is null, throw an ArgumentNullException.
Iterate the
source sequence.
Check to see if this element value has already been returned (Uses the comparer function provided for equality evaluation)
If this element value hasn't been returned, Add it to the list of those that has and Return the current element (pick up from here when the next element is requested).
EditLoop Count
This operator implements the standard
deferred execution iterator pattern. This means, no looping will occur until the result is iterated over..
EditCode Samples
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();
}
Century 21 Broker Properti Jual Beli Sewa Rumah Indonesia
Peter
Paul
Mary
Janet