LINQ Standard Query Operators

linq_480x400_acf_cropped

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 interface or the IQueryable 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 operates on objects of type IEnumerable and the other set operates on objects of type IQueryable. 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, operate on in-memory collections. The query methods which are extended from IQueryable, 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 an anonymous method, like Where():

public static IEnumerable Where(

this IEnumerable source, Func<TSource, bool> predicate)

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

LINQ Query Method Execution

public static IQueryable Where(

this IQueryable source, Expression<Func<TSource, bool>> predicate)

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);

}
  1. Words of length 3:

  2. THE

  3. FOX

  4. THE

  5. DOG

  6. Words of length 4:

  7. OVER

  8. LAZY

  9. Words of length 5:

  10. QUICK

  11. BROWN

  12. JUMPS

  13. */

Catch us on social media

Author Profile

Deependra Kushwah
Deependra Kushwah
Deependra is a Senior Developer with Microsoft technologies, currently working with Opteamix India business private solution. In My Free time, I write blogs and make technical youtube videos. Having the good understanding of Service-oriented architect, Designing microservices using domain driven design.

Deependra Kushwah

Deependra is a Senior Developer with Microsoft technologies, currently working with Opteamix India business private solution. In My Free time, I write blogs and make technical youtube videos. Having the good understanding of Service-oriented architect, Designing microservices using domain driven design.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

RSS
Facebook
Google+
https://betechnical.blog/2018/01/14/linq-standard-query-operators">
Twitter
YouTube
Pinterest
Pinterest
LinkedIn
Instagram
%d bloggers like this: