C# LINQ Queryable.Except Method in ASP.Net
The Except method of System.Linq.Queryable class enables to get the elements of first sequence that are not available in the second sequence. In actual it produces a difference of two sequences but returns the elements of first sequence. By default the Except method compares the values using default comparer if the type of elements belongs to base data types such as int, string, decimal etc. If the elements are of custom types then you have to implement the IEquatable<T> interface to the class for comparing the values. The LINQ Except method has the following two overloaded functions which accept one or two parameters and produce the output accordingly:
1.
public static IQueryable<TSource> Except<TSource>(this IQueryable<TSource> first, IQueryable<TSource> second);
It returns the elements of first sequence that are not available in second sequence. It compares the values using default comparer or IEquatable comparer defined for the custom types.
2.
public static IQueryable<TSource> Except<TSource>(this IQueryable<TSource> first, IQueryable<TSource> second, IEqualityComparer<TSource> comparer);
It returns the elements of first sequence that are not available in second sequence b y comparing the values using the specified IEqualityComparer.
C# LINQ Except Method Example in ASP.Net
// Example 1:
string[] fruits1 = { "oranges", "grapes", "guava", "plums", "raspberries", "gooseberries", "pineapples" };
string[] fruits2 = { "plums", "raspberries", "gooseberries" };
// implementing Except method
IQueryable<string> fruits = fruits1.AsQueryable().Except(fruits2);
foreach (var fruit in fruits)
{
Response.Output.Write("{0}<br />", fruit);
}
// Example 2:
List<Product> products1 = new List<Product>()
{
new Product{ ProductName = "Butter", ProductID = 1 },
new Product{ ProductName = "Cheese", ProductID = 2 },
new Product{ ProductName = "Chocolate", ProductID = 3 },
};
List<Product> products2 = new List<Product>()
{
new Product{ ProductName = "Cheese", ProductID = 2 },
};
// implementing Except method
IQueryable<Product> query1 = products1.AsQueryable().Except(products2);
foreach (var product in query1)
{
Response.Output.Write("{0} {1}<br />", product.ProductName, product.ProductID);
}
// Example 3:
// implementing Except method
IQueryable<Product> query2 = products1.AsQueryable().Except(products2, new ProductComparer());
foreach (var product in query2)
{
Response.Output.Write("{0} {1}<br />", product.ProductName, product.ProductID);
}
// Output:
// oranges
// grapes
// guava
// pineapples
// Butter 1
// Chocolate 3
// Butter 1
// Chocolate 3
C# Product Class:
public class Product : IEquatable<Product>
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public bool Equals(Product other)
{
// check whether the compared object is null
if (Object.ReferenceEquals(other, null))
return false;
// check whether compared objects reference the same data
if (Object.ReferenceEquals(this, other)) return true;
// check whether the properties are equal
return this.ProductID == other.ProductID && this.ProductName == other.ProductName;
}
public override int GetHashCode()
{
// get hash code for product name
int productNameHashCode = this.ProductName == null ? 0 : this.ProductName.GetHashCode();
// get hash code for product id
int productIdHashCode = this.ProductID.GetHashCode();
// calculate the hash code for the object
return productIdHashCode ^ productIdHashCode;
}
}
public class ProductComparer : IEqualityComparer<Product>
{
public bool Equals(Product x, Product y)
{
// check whether both the objects reference the same data
if (Object.ReferenceEquals(x, y)) return true;
// check whether any of the object is null
if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
return false;
// check whether the properties are equal
return x.ProductID == y.ProductID && x.ProductName == y.ProductName;
}
public int GetHashCode(Product obj)
{
// check whether the object is null
if (Object.ReferenceEquals(obj, null)) return 0;
// get hash code for product name
int productNameHashCode = obj.ProductName == null ? 0 : obj.ProductName.GetHashCode();
// get hash code for product id
int productIdHashCode = obj.ProductID.GetHashCode();
// calculate the hash code for the object
return productIdHashCode ^ productIdHashCode;
}
}
In the above C# code sample we have given three examples for LINQ Except method. In first example we have implemented the Except method over two string type arrays to get the element from first array sequence that are not available in second one. In the next examples we have applied the Except method over the sequences containing Product class objects. The second example will compare the values of using IEquatable comparer to produce the result whereas third example will compare the values using IEqualityComparer.
Continue to next tutorial: C# LINQ Queryable.First Method in ASP.Net to learn how to get the first element of a sequence.

* will not be published
* hint: http://www.example.com