Distinct Operator Part 2

Distinct operator removes duplicate elements from input sequence.

The prototype for distinct operator looks like this

public static IEnumerable<T> Distinct<T>(
this IEnumerable<T> source);

Distinct operator basically enumerates over a sequence and returns objects that has not been previously returned. In order to determine if two items are same it uses gethashcode and equals method to identify if two values are the same.

Lets look at a simple example distinct operator to identify its usage.

image

 

CWindowssystem32cmd.exe

 

Looking at the above example, you would notice that we have several numbers that are duplicates. By making use of the distinct operator we are only returning numbers that are not duplicate. 

The same distinct concept can be applied to linq to sql. Lets look at an example of that.

image

In the above query, we are grab all the customerid for every order by traversing the customer property of the order. Since there will be many orders placed by a single customer, we apply a distinct operator to retrieve only unique customers. 

Unfortunately there is no equivalent syntax of writing distinct in a query syntax and you have to resort to the method syntax to apply distinct operator.

One of the overload of distinct operator makes use of IEqualityComparer. What this allows you to do, is define your own implementation of wether two objects are equal or not. When you create your own IEqualityComparer, you basically have to implement two methods GetHashCode and Equals. Here is an example that makes use of IEqualityComparer to customize how two objects are considered equal.

image

 

image

 

CWindowssystem32cmd.exe (2) 

Looking at our NameComparer class you would notice that the way we are defining how two names are equal, is by saying if the firstname for each name is same, then the names are considered equal. Then in our distinct operator we pass in that NameComparer which forces the query to return only two results because first names of both Zeeshan Jafar and Zeeshan Hirani happens to be the same.

No Comments