Easy way to understand LINQ
There are many videos and tutorials on LINQ. And i am adding one more to the list. I am not going deeply in LINQ but just giving basic syntax understanding of LINQ.
Following is the description of LINQ
from [identifier] in where [boolean expression] order by [[expression](ascending/descending)], [optionally repeat] select [expression] group [expression] by [expression] into [expression]
These LINQ query returns the IEnumerable collection. Let us go line by line into the same.
First line is similar to declaring variable. Here you are declaring variable for your data collection. But this data collection is filter of your source collection. Let’s take one example
Now, here c is variable and it is subset or equal to contacts. So whenever you have collection from which you want to make queries, first line would be from <Identifier> in <collection>
From next line everybody use expression (Not facial but technical 🙂 ). This expression must use identifier you have defined in the first line. So in above example, you can see that WHERE and ORDERBY uses c to create expression. c has same members which your source has. Suppose you have contact collection named contacts. And you wrote first line like “from c in contacts” then c contain all the members of contact. Visual studio provide Intellisense using which you can easily create the expression. Where takes boolean expression, while order by take just simple expressions.
Once you have setup the LINQ query, you need to select the collection. It would be simple identifier or may be columns from identifier. One more example for understanding is as follows:
This is just a very basic understanding on LINQ. But its really vast and if you need to go in bit detail, follow http://www.hookedonlinq.com/MainPage.ashx. Nice website for LINQ :).