C# Program to Demonstrate the Example of LINQ Intersect() Method with OrderBy() Method
Last Updated :
06 Dec, 2021
LINQ is known as Language Integrated Query and it is introduced in .NET 3.5. It provides the power to .NET languages to create queries to retrieve data from the data source. In this article we will demonstrate the example of the LINQ intersect() method with OrderBy() method.
1. intersect() Method: This is used to get the common elements from the two given lists. Or we can say that this method returns the intersection of two lists. It is available in both the Queryable and Enumerable classes.
Syntax:
data1.Intersect(data2)
Where data1 is the first list and data2 is the second list.
2. OrderBy() Method: The sole purpose of this method is to sort the given list of elements in ascending order. When you write the LINQ query using this method you need not write extra conditions to sort the array in ascending order.
Syntax:
data1.OrderBy(i => i)
Where i is the iterator.
Now, we are combining the intersect() and OrderBy() method by first getting common elements using intersect() function, and then from the result we are getting the data in the ascending order with OrderBy() function and display the data using the iterator. So to do this we use the following query:
data1.Intersect(data2).OrderBy(i => i);
Where data1 is the first list and data2 is the second list
Example:
Input: { 10, 20, 30, 40, 50, 60, 70 }
{ 50, 60, 70, 80, 90, 100 }
Output:
50
60
70
Input: { 10, 20, 30 }
{ 50, 60, 70 }
Output:
No Output
Approach:
1. Create and initialize two lists of integer type named as data1 and data2.
2. Now we use the below query to find the common elements from these lists and then sort them in ascending order.
final = data1.Intersect(data2).OrderBy(i => i);
3. Iterate the results using foreach loop.
foreach (var j in final)
{
Console.WriteLine(j + " ");
}
Example:
C#
// C# program to illustrate how to use Intersect() method
// with OrderBy() method in LINQ
using System;
using System.Linq;
using System.Collections.Generic;
class GFG{
static void Main(string[] args)
{
// Create first list
List<int> data1 = new List<int>(){
10, 20, 30, 40, 50, 60, 70 };
// Create second list
List<int> data2 = new List<int>() {
50, 60, 70, 80, 90, 100 };
// Finding the intersection of two lists and
// then order them in ascending order.
var final = data1.Intersect(data2).OrderBy(i => i);
// Display the numbers
Console.WriteLine("Final Result is: ");
foreach(var j in final)
{
Console.WriteLine(j + " ");
}
}
}
Output:
Final Result is:
50
60
70
Similar Reads
C# Program to Demonstrate the Example of LINQ Union() Method with StringComparer LINQ is known as Language Integrated Query and it is introduced in .NET 3.5. It gives a feature to .NET languages to create queries to retrieve data from the data source. Here in this article, we will demonstrate the example of the LINQ Union() method with the StringComparer. 1. Union() Method: This
2 min read
C# Program to Sort a List of Integers Using the LINQ OrderBy() Method Given a list of integers, now our task is to sort the given list of integers. So we use the OrderBy() method of LINQ. This method is used to sort the elements in the collection in ascending order. This method is overloaded in two different ways: OrderBy<TSource, TKey>(IEnumerable<TSource
2 min read
C# Program to Demonstrate the IList Interface In C# IList interface is an interface that belongs to the collection module where we can access each element by index. Or we can say that it is a collection of objects that are used to access each element individually with the help of an index. It is of both generic and non-generic types. Its implem
3 min read
C# Program to Sort a List of String Names Using the LINQ OrderBy() Method Given a list of string names, now our task is to sort the names given in the list using the OrderBy() method of LINQ. This method is used to sort the elements in the collection in ascending order. While using this method in the LINQ query you don't need to add an extra condition to sort the list in
2 min read
C# Program to Demonstrate the IDictionary Interface IDictionary Interface is an interface that belongs to the collection module where we can access the elements by keys. Or we can say that the IDictionary interface is a collection of key/value pairs. It is available for both generic and non-generic types collection. Here each pair must contain a uniq
3 min read
C# Program to Show the Usage of LINQ Aggregate() Method In LINQ, the aggregation function is the function that serves the purpose of calculating one value from a collection of values. Or we can say that the Aggregate() method is used to perform aggregation operations on the values of a collection. In simple words, the Aggregate() method implements a numb
2 min read
C# Program to Sort Student Names in Descending Order using LINQ Given a list of student names, now our task is to sort the names given in the list in descending order using the LINQ, This task can be done using OrderByDescending() method of LINQ. This method is used to sort the elements in the collection in descending order. While using this method in the LINQ q
2 min read
C# Program to Find the Index of Even Numbers using LINQ Given an array, now our task is to find the index value of the even numbers present in the given array using LINQ. LINQ is known as Language Integrated Query and was introduced in .NET 3.5. It gives the power to .NET languages to generate queries to retrieve data from the data source. So to do this
2 min read
C# Program to Sort a List of Employees Based on Salary in Descending Order and Whose Department is XYZ using LINQ Given a list of employees, now we sort the list according to the salary in descending order and whose department is XYZ. So we use the OrderByDescending() method of LINQ along with the Where() method. The OrderByDescending() is used to sort the specified list in descending order. To solve the given
2 min read
C# Program to Sort a List of Employees Based on Salary using LINQ Given a list of employees, now we sort the list of employees according to their salary using LINQ. So to this task, we use the OrderBy() method. This method is used to sort the elements of the specified sequence in ascending order. Example: Input: {id = 101, Name = Rohit, Salary = 50000, Department
3 min read