How to LINQ? Part-4: Standard Query Operators

Having covered the query expressions, we need to look in to query operators. The standard query operators are the methods that form query pattern.  Note that these methods operate on any object whose type implements the IEnumerable<T> or IQueryable<T> interfaces.

 

The methods that make up these standard query operators are implemented as Extension Methods. To understand this, some knowledge on Extension Methods introduced by C# are required. Refer msdn for more details.

 

Here is the summary of the LINQ query operators:

 

  1. Aggregation: Average, Count, LongCount, Max, Min, Sum
  2. Concatenation : Concat
  3. Conversion: Cast, OfType, ToArray, ToDictionary, ToList, ToLookup, ToSequence
  4. Element :  DefaultIfEmpty, ElementAt,  ElementAtOrDefault, First, FirstOrDefault, Last, LastOrDefault, Single, SingleOrDefault
  5. Equality : SequenceEqual
  6. Generation : Empty, Range, Repeat
  7. Grouping : GroupBy
  8. Joining : GroupJoin, Join
  9. Ordering:  OrderBy, ThenBy, OrderByDescending, ThenByDescending, Reverse
  10. Partitioning : Skip, SkipWhile, Take, TakeWhile
  11. Quantifiers:  All, Any, Contains
  12. Restriction : Where
  13. Projection: Select, SelectMany
  14. Set:  Distinct, Except, Intersect, Union

For the detailed information on this topic you can look at this msdn resource. The links below will help you to navigate to each set of operations directly.

Sorting | Set  | Filtering | Quantifier | Projection | Partitioning | Join | Grouping | Generation | Equality | Element | Conversion | Concatenation  |Aggregation

How to LINQ? Part-2: Query Expressions

Query Expressions

 

The main learning curve for an application developer who is new to LINQ is the query syntax. This declarative syntax requires minimum code by which you can even perform fairly complex queries. Let us look at some code snippets for example:

 

A simple select query – Selecting the marks from the array which are above 50

 

int[] marks  = { 85, 40, 75, 60 };

 

var passedMarks =
from m in marks

where m>50
select m;

 

Console.WriteLine(“Selected Marks :”);
    foreach (var i in passedMarks) {
        Console.WriteLine(i);
    }

 

Output:

 

Selected Marks :

85

75

60

 

 

 

MSDN tells about the three distinct parts of a LINQ query

 

  1. Obtain the data source.
  2. Create the query.
  3. Execute the query

 

Let us relate this to the first example:

 

Obtain the data source

int[] marks  = { 85, 40, 75, 60 };

 

Create the query

var passedMarks =
from m in marks

where m>50
select m;

 

Execute the query

Console.WriteLine(“Selected Marks :”);
    foreach (var i in passedMarks) {
        Console.WriteLine(i);
    }

 

How to LINQ? Part-3: 101 LINQ Samples

LINQ Query Samples

 

You have seen basics of a LINQ query in my previous blog. It is time to look at some more samples. There are numerous resources in the web, but many of you must be interested in a single resource where you can get ‘everything’. Well, here is one msdn resource which puts together numerous examples on the following topics

 

Linq to Objects

Linq to Sql

Linq to DataSet

Linq to Xml

How to LINQ? Part-1: Understanding the basics of LINQ

What is LINQ?

 

One of the key additions in .NET Framework 3.5 is LINQ (Language Integrated Query).  In the early days it was a little confusing for many since it is a different model. Here is an attempt to put it in a simple way and see what the capabilities that LINQ provide are.

 

LINQ adds powerful query capabilities to programming language syntax (C#, Visual basic). It provides some patterns for querying and updating data from different data sources.

 

What does that mean?  With the introduction of LINQ, “query” becomes a basic language construct, just like control structures, classes, methods etc.

 

LINQ Providers

 

As mentioned above, the query can be against different data sources. It can be used with .NET framework objects, SQL Databases, ADO.NET Datasets and XML documents. These different providers define different flavours of LINQ that are listed below:

 

  1. LINQ to Objects
  2. LINQ to XML
  3. LINQ to SQL
  4. LINQ to Datasets

Before getting into details of these, we should understand the basic query expressions which I will focus in the next blog in this series.