Blogger news

Blogger templates

Friday, February 4, 2011

WHERE- LINQ OPERATOR

LINQ Operators
LINQ operators are extension method, extend IEnumerable. So we will get LINQ operators because all collection internally implements IEnumerable interface. we can categorize LINQ operators into these categories,

Restriction Operators
Projection Operators
Partitioning Operators
Join Operators
Concatenation Operators
Ordering Operators
Grouping Operators
Set Operators
Conversion Operators
Equality Operators
Element Operators
Generation Operators
Quantifiers
We will discuss one by one.

Restriction Operators

Where

The Where operator filters a sequence(sequence is a collection and element is each element in that sequence) based on a predicate(a bool condition). Where has two overloads,


see the parameter of "Where" in the above image,

"this IEnumerable<TSource> source " meaning of this is "Where" is an extension method which extend the interface IEnumerable<>. so we will get "Where" with any type which implements IEnumerable<> interface. "this IEnumerable<TSource> source" is not a method parameter.

Func<TSource,bool> predicate- means this is a method parameter of delegate type and that delegate expects a method with input parameter TSource (it would be a collection of any type) and return a bool value. which means a method like this,
 

public static bool FuncMethod(IEnumerable<string> names)
{
return true;
}
or we can write Lambda for the same as,
Func<IEnumerable<string>, bool> fun = a => true;.

We will go ahead with Lambda expression.
I will explain "Where" with an example. Here i am writing a name collection,

List<string> names = new List<string> { "Sreelas", "Serosh", "Mohandas", "Shareef", "Ansar" };

var filteredNames = names.Where(a => a == "Serosh");

foreach (var filteredName in filteredNames)
{
Console.WriteLine(filteredName);
}

see the line a => a == "Serosh" this is "Func" because it has a input parameter and return a value. it matching with the signature of "Func" in "Where" operator.

we can avoid the "foreach" loop with,


names.Where(a => a == "Serosh").ToList().ForEach(b => Console.WriteLine(b));
will discuss the above way in depth later.
so where returns the filtered according to condition. So our output would be "Serosh".


Next I will explain with another example. Say I have an Associate class as below,

And I have a collection of associate entities as below,

Click image to expand.

Now I want output of associate where AssociateId=101 and print that output to console. We can write filtering and printing in a single line as,
Click image to expand.

Above code is nothing but complete lambda expression form of,

Click image to expand.



The output would be,
Let us check "Where" operator in this example ,

Click image to expand


So "Func" in the "Where" operator of this example expecting an input parameter of type "Associate" and returns "bool that why we implemented Lambda as,
a => a.AssociateId == 101, here "a" is the input parameter of type "Associate" and "a.AssociateId == 101" returns bool.
when we disassemble "System.LINQ" dll for "Where" it looks like,

In the above lambda the variable "a" is of type "Associate" that's why we get properties of Associate class with "a". Compiler can understand the type of "a" by looking the type of the collection, "associates". "associates" is a collection of class "Associate" so compiler can understand the type of "a" is Associate.

No comments:

Post a Comment