HI WELCOME TO SIRIS

Understanding LINQ Standard Query Operators

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():
  1. public static IEnumerable<TSource> Where<TSource>(
  2. 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():
  1. public static IQueryable<TSource> Where<TSource>(
  2. 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)

  1. string sentence = "The quick brown fox jumps over the lazy dog";
  2.  
  3. // Split the string into individual words to create a collection.
  4. string[] words = sentence.Split(' ');
  5.  
  6. // Using query expression syntax.
  7. var query = from word in words
  8. group word.ToUpper() by word.Length into gr
  9. orderby gr.Key
  10. select new { Length = gr.Key, Words = gr };
  11.  
  12. //You can also write above query by using method-based query syntax.
  13. var query1 = words.
  14. GroupBy(w => w.Length, w => w.ToUpper()).
  15. Select(g => new { Length = g.Key, Words = g }).
  16. OrderBy(o => o.Length);
  17.  
  18. //use either query or query1 variable to get result
  19. foreach (var obj in query)
  20. {
  21. Console.WriteLine("\n Words of length {0}:", obj.Length);
  22. foreach (string word in obj.Words)
  23. Console.WriteLine(word);
  24. }
  25.  
  26. /*output:
  27. Words of length 3:
  28. THE
  29. FOX
  30. THE
  31. DOG
  32. Words of length 4:
  33. OVER
  34. LAZY
  35. Words of length 5:
  36. QUICK
  37. BROWN
  38. JUMPS
  39. */

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.