Thursday, October 6, 2011

Building complex queries using LINQ


The most important things to be remembered for this example are:- 

1) All types are defined in System.Linq and System.Linq.Expressions namespaces.
2) A query operator never modifies/alters the input sequence, instead it always returns a 
    new sequence.
3) In a query expression data always flows from left to right through the sequence of 
     operators.(will be explaining this in the example)

In the given example multiple query operators (Where, OrderBy and Select) will be used in a query expression to build complex query.

At first a brief description about the Where, OrderBy and Select query operators is necessary, to give an insight view to what we are dealing with.

Where - The Where operator filters a sequence based on a predicate.When the object 
                returned by Where is enumerated, it enumerates the source sequence and yields 
                those elements for which the predicate function returns true.

OrderBy - OrderBy operator order a sequence according to one or more keys.

Select - The Select operator performs a projection over a sequence.When the object 
               returned by Select is enumerated, it enumerates the source sequence and yields the 
               results of evaluating the selector function for each element.

Complex query Example :-

string[] strInputSequence = { "london", "new york", "sydney", "new delhi" };
var outputSequence = strInputSequence
                     .Where(n => n.Length>5 && n.Contains("ne")) // filteration
                     .OrderBy(n => n)  //sorting
                     .Select(n => n.ToUpper()); //upper case conversion
foreach (string name in outputSequence) 
{
    Console.WriteLine(name);
}

Output
-------------
NEW DELHI
NEW YORK
SYDNEY

In the above example Where operator returns a filtered version of the input sequence, which becomes a input to the OrderBy operator. The OrderBy operator returns sorted version of its input sequence which becomes a input sequence to the Select operator. And finally Select operator returns a sequence where each element is converted into upper case using the lambda expression.

The same desired result can be achieved by breaking the single complex query into separate queries for better understanding.

string[] strInputSequence = { "london""new york""sydney""new delhi" };

var collfilter = strInputSequence.Where(n => n.Length > 5 && n.Contains("ne"));
foreach (string n in collfilter)
{
     Console.WriteLine(n);
}

Output
---------
new york
sydney
new delhi


var collOrderby = collfilter.OrderBy(n => n);
foreach (string n in collOrderby)
{
      Console.WriteLine(n);
}

Output
----------
new delhi
new york
sydney


var finalOutput = collOrderby.Select(n => n.ToUpper());
foreach (string n in finalOutput)
{
     Console.WriteLine(n);
}

Output
------------
NEW DELHI
NEW YORK
SYDNEY

 By using the above individual queries it can be easily understood that when the operators are chained in a query then the output sequence of one operator is the input sequence of another operator.


[ Following link can be referred to get in depth knowledge of query operators
   http://msdn.microsoft.com/en-us/library/bb394939.aspx ]


Happy coding......

No comments :

Post a Comment