English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The following table lists all the Set operators available in LINQ.
Set Operators | Usage |
---|---|
Distinct | Returns non-repetitive values in the collection. |
Except | Returns the difference between two sequences, which means elements not appearing in the second collection. |
Intersect | Returns the intersection of two sequences, which means elements appearing in both collections. |
Union | Returns unique elements from two sequences, which means unique elements appearing in both sequences. |
The Distinct extension method returns a new collection of unique elements from the given collection.
IList<string> strList = new List<string>(){ "One", "Two", "Three", "Two", "Three" }; IList<int> intList = new List<int>(){ 1, 2, 3, 2, 4, 4, 3, 5 }; var distinctList1 = strList.Distinct(); foreach (var str in distinctList1) Console.WriteLine(str); var distinctList2 = intList.Distinct(); foreach (var i in distinctList2) Console.WriteLine(i);
One Two Three 1 2 3 4 5
The Distinct extension method does not compare the values of complex type objects. To compare the values of complex types, you need to implement the IEqualityComparer<T> interface. In the following example, the StudentComparer class implements IEqualityComparer<Student> to compare Student objects.
public class Student { public int StudentID { get; set; } public string StudentName { get; set; } public int Age { get; set; } } class StudentComparer : IEqualityComparer<Student> { public bool Equals(Student x, Student y) { if (x.StudentID == y.StudentID && x.StudentName.ToLower() == y.StudentName.ToLower()) return true; return false; } public int GetHashCode(Student obj) { return obj.StudentID.GetHashCode(); } }
Now, you can pass an instance of the StudentComparer class as a parameter to the Distinct() method to compare Student objects, as shown below. Example: Distinct comparison object in C#
IList<Student> studentList = new List<Student>() { new Student() { StudentID = 1, StudentName = "John", Age = 18 }, new Student() { StudentID = 2, StudentName = "Steve", Age = 15 }, new Student() { StudentID = 3, StudentName = "Bill", Age = 25 }, new Student() { StudentID = 3, StudentName = "Bill", Age = 25 }, new Student() { StudentID = 3, StudentName = "Bill", Age = 25 }, new Student() { StudentID = 3, StudentName = "Bill", Age = 25 }, new Student() { StudentID = 5, StudentName = "Ron", Age = 19 } }; var distinctStudents = studentList.Distinct(new StudentComparer()); foreach(Student std in distinctStudents) Console.WriteLine(std.StudentName);
John Steve Bill Ron
C# query syntax does not support the Distinct operator. However, you can use the Distinct method to query a variable or wrap the entire query in parentheses and then call Distinct().
Using the Distinct keyword in VB.Net query syntax:
Dim strList = New List(Of string) From {"One", "Three", "Two", "Two", "One"} Dim distinctStr = From s In strList _ Select s Distinct