top of page
Writer's picturehr9083

LINQ in C#

LINQ (Language-Integrated Query) in C# is a powerful feature that allows developers to query data from different data sources using an SQL-like syntax directly within C# code. It provides a uniform way to query and manipulate data from various sources like collections, arrays, databases, XML, and more. 






Distinct : 

The Distinct method in LINQ is used to eliminate duplicate elements from a sequence, returning only distinct elements. It can be applied to various types of collections, including lists, arrays, and other enumerable sequences. 


For Example: 



 

 












Output: 

 1, 2, 3, 4, 5, 7, 6, 8(It eliminates the duplicate number) 

 


Filtering : 

Filtering in LINQ refers to the process of selecting elements from a data source based on certain criteria. LINQ provides several methods and operators for filtering data. 

 

Let's take an example to understand Filtering: 



 
















Output:   

Even numbers are: 2, 4, 6, 8 

Odd numbers are: 1, 3, 5, 7 

 


Sorting: 

Sorting refers to arranging elements in a collection or sequence in a specific order (Ascending and Descending) based on one or more criteria. 

 

Let's take an example to understand Sorting:  



 

 

 











Output: 

 Sorted list in Ascending order is : 1, 2, 3, 4, 5, 6, 7, 8( 

 Sorted list in Descending order is : 8, 7, 6, 5, 4, 3, 2,  

 


First & FirstOrDefault : 

The First() method returns the first element in a sequence that satisfies the specified condition. 

If no element in the sequence satisfies the condition, the method throws an exception (System.InvalidOperationException). 

This method is commonly used when you expect the sequence to have at least one element that meets the condition and you want to retrieve the first one. 

 

Let's take an example to understand:  



 

 

 











Output:  

First Even number is: 2 

 


Last & LastOrDefault: 

Last and LastOrDefault is also do the same as First and FirstOrDefault but in reverse. 

 

Let's take an example to understand:  

 



 

 














Output: 

 First Even number is: 8 

 First Even number is: List is empty 

 


Any: 

The Any method in LINQ is used to check if any element in a sequence satisfies a specified condition. It returns true if any element in the sequence satisfies the condition; otherwise, it returns false. 

 

Let's take an example to understand: 



 

 

 















Output: 

 First number is Even ? Answer is: True 

 First number is Even ? Answer is: False 

 


All: 

 The All method in LINQ is used to check if all elements in a sequence satisfy a specified condition. It returns true if all elements in the sequence satisfy the condition; otherwise, it returns false.  


 Let's take an example to understand:



 

  














Output:     

 All numbers are Even in first list ? Answer is: False 

 All numbers are Even in second list ? Answer is: True 

 

 

 

 

  

Skip & Take: 

In LINQ, the Skip and Take methods are used to skip a specified number of elements from the beginning of a sequence (Skip) or to take a specified number of elements from the beginning of a sequence (Take). These methods are often used for pagination or to retrieve a subset of data from a larger collection. 

 

Let’s understand with an example: 



 

 














Output:           

take first 3 from the list is: 1, 2, 3 

Skip 5 elements from the list: 6, 7, 8 

 


Zip: 

The Zip method in LINQ is used to merge two sequences by pairing elements with the same index from each sequence. It takes two input sequences and applies a specified function to corresponding elements from the two sequences to produce the result. The resulting sequence will have a length equal to the shorter of the two input sequences. 

 

Let’s understand with an example: 



 

 

 













Output: 

  1-a, 2-b, 3-c 

 


Aggregate:  The Aggregate method in LINQ is used to perform a custom aggregation operation over the elements of a sequence. It takes an initial seed value and a function that defines how to combine the seed value with each element of the sequence. The result of the aggregation is the final accumulated value.  

Let’s understand with an example: 

 



 

 

 











Output:  

36 

 


Select & From: 

Select Clause: The select clause in LINQ is used to project data from a data source into a new form. It transforms the elements of the source collection, array, or query results to produce a new sequence or result set. It allows you to specify what data you want to retrieve or transform from the source data. 


From Clause: The from clause in LINQ is used to specify the data source from which you want to query data. It indicates the source collection, array, or range variable over which you want to perform LINQ operations. It's often the starting point of a LINQ query, where you declare the range variable that represents each element in the data source. 

 

 

Let’s understand with an example: 



 

 

 












Output: 

Fruits starting with 'A': 

Apple 

 

  

SelectMany: 

The SelectMany method is used to flatten a sequence of sequences (or collections of collections) into a single sequence. It applies a projection function to each element of the input sequence and flattens the resulting sequences into one sequence. 

 

Let’s understand with an example: 

 





















Output:  

Flattened List: 

1  


0 views0 comments

Recent Posts

See All

Comments


bottom of page