Please enable Javascript to correctly display the contents on Dot Net Tricks!

Understanding LINQ Standard Query Operators

  Author : Shailendra Chauhan
Updated On : 17 Jul 2014
Total Views : 63,903   
 

LINQ standard query operators are the methods which help you to write LINQ query. Most of these query methods operate on sequences or collection whose implement the IEnumerable<T> interface or the IQueryable<T> interface. These operators provide query capabilities including filtering, projection, aggregation, sorting and much more.

LINQ Standard Query Operators Sets

LINQ provides two sets of standard query operators, one set operate on objects of type IEnumerable<T> and the other set operate on objects of type IQueryable<T>. Also, these query methods (standard query operators) are static members of the Enumerable and Queryable static classes They are defined as extension methods of the type on which they operate. Hence, they are called either by using static method syntax or instance method syntax.

The query methods which are extended from IEnumerable<T>, operate on in-memory collections. The query methods which are extended from IQueryable<T>, operate on out-of-process memory collections and build an expression tree that represents the query to be performed.

Example for LINQ Query Method Where() Sets

In LINQ to Objects (in-memory collection), LINQ query methods would be used as anonymous method, like Where():

public static IEnumerable<TSource> Where<TSource>(
 this IEnumerable<TSource> source, Func<TSource, bool> predicate)

While in LINQ to SQL (out-memory collection), mostly LINQ query methods would be used as expression tree, like Where():

public static IQueryable<TSource> Where<TSource>(
 this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate) 

LINQ Query Method Execution

The standard query operators are executed based on the value they return. The query methods that return a singleton value (like Average, Sum, Min, Max etc.) then execute immediately. The query methods that return a sequence or collection defer the query execution. The query methods can be chained together in one query.

Writing LINQ Query with the help of Standard Query Operators (LINQ Query Methods)

string sentence = "The quick brown fox jumps over the lazy dog";

// Split the string into individual words to create a collection. 
string[] words = sentence.Split(' ');

// Using query expression syntax. 
var query = from word in words
 group word.ToUpper() by word.Length into gr
 orderby gr.Key
 select new { Length = gr.Key, Words = gr };

//You can also write above query by using method-based query syntax. 
var query1 = words.
 GroupBy(w => w.Length, w => w.ToUpper()).
 Select(g => new { Length = g.Key, Words = g }).
 OrderBy(o => o.Length);

//use either query or query1 variable to get result
foreach (var obj in query) 
{
 Console.WriteLine("\n Words of length {0}:", obj.Length);
 foreach (string word in obj.Words)
 Console.WriteLine(word);
}

/*output: 
 
 Words of length 3: 
 THE 
 FOX 
 THE 
 DOG 
 
 Words of length 4: 
 OVER 
 LAZY 
 
 Words of length 5: 
 QUICK 
 BROWN 
 JUMPS 
*/

Different types of standard query operators

Operator Type
Operator Name
Aggregate
Aggregate, Average, Count, LongCount, Max, Min, Sum
Concatenation
Concat
Conversions
Cast, OfType, ToList, ToArray, ToLookup, ToDictionary, AsEnumerable
Element
Single, SingleOrDefault, First, FirstOrDefault, Last, LastOrDefault, ElementAt, ElementAtOrDefault
Equality
SequenceEqual
Generation
Repeat, Range, Empty
Grouping
GroupBy
Join
Join, GroupJoin
Ordering
OrderBy, OrderByDescending, ThenBy, ThenByDescending, Reverse
Partitioning
Skip, SkipWhile, Take, TakeWhile
Projection
Select, SelectMany
Quantifier
Contains, All, Any
Restriction
Where
Set
Union, Intersect, Except, Distinct
What do you think?

I hope you will enjoy LINQ Standard Query Operators while programming with LINQ. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

YOU MIGHT LIKE
Free Interview Books
 
COMMENTS (0)
14 DEC
ASP.NET MVC with AngularJS Development (online)

MON-FRI 07:30 AM- 09:00 AM IST

Know More
5 DEC
AngularJS Development (online)

Mon - Fri 6:30 AM-7:30 AM IST

Know More
3 DEC
AngularJS Development (offline)

SAT,SUN     11:00 AM-12:30 PM IST

3 DEC
MEAN Stack Development (offline)

Sat, Sun     (09:30 AM-11:00 AM IST)

26 NOV
ASP.NET MVC with AngularJS Development (offline)

(SAT,SUN)     03:30 PM-05:00 PM IST

24 NOV
ASP.NET MVC with AngularJS Development (online)

MON-FRI     09:30 PM-11:00 PM IST

12 NOV
ASP.NET MVC with AngularJS Development (offline)

SAT,SUN     08:00 AM-09:30 AM

3 NOV
ASP.NET MVC with AngularJS Development (online)

MON-FRI     07:30 AM-09:00 AM IST

25 OCT
.NET Development (offline)

Mon-Fri     9:00 AM-11:00 AM IST

BROWSE BY CATEGORY
 
RECENT ARTICLES
SUBSCRIBE TO LATEST NEWS
 
LIKE US ON FACEBOOK
 

Professional Speaks

+