LINQ (Language Integrated Query) - Part 5
This is fifth part of the ‘LINQ’ series posts that I have started from here. In the last post we talked on how to setup a demo project to explore LINQ queries, I expect you too have a demo project with you, if not then visit Part 4 of this series and setup it. Let’s go ahead and explore selecting records using LINQ and explore internals.
I hope you will find it useful. Thanks for reading.
Selecting data using LINQ queries is very simple, you will get full intellisense support while querying database and even compile time error check that adds great advantage to LINQ, once you learn it, you will love to use this in your every DB projects. I’m loving this, it is great.
In the image given below, you will find the sample code for selecting employees ‘FirstName’ and ‘LastName’ from ‘Employee’ table of ‘Northwind’ Database:
In above image, you can see, I’m using same queries two times, but there is difference, don’t go on output screen only. If you look on the code carefully, you will find I’m using IEnumerable<Employee> in the first block of code and just a ‘var’ in second block of code. Also note, for IEnumerable<Employee> I’m using ‘Employee’ in foreach loop and for ‘var’ I’m using ‘var’ in foreach loop, you should care it always.
Now, what is the difference between ‘IEnumerable<Employee>’ and ‘var’?
When you see a query variable that is typed as IEnumerable<T>, it just means that the query, when it is executed, will produce a sequence of zero or more T objects, as given in image blow (just hover the mouse on ‘empQuery1’ when compiler is on break). When you expand the index, you will find entire data associated with that row/record.
If you prefer, you can avoid generic syntax like IEnumerable<T> by using the var keyword. The var keyword instructs the compiler to infer the type of a query variable by looking at the data source specified in from clause, nothing big difference you still get every benefits.
Now, let’s talk on the query part.
= from emp in NDC.Employees
In C# as in most programming languages a variable must be declared before it can be used. In a LINQ query, the ‘from’ clause comes first in order to introduce the data source (NDC.Employees) and the range variable (emp). NDC is just an instance of the ‘Northwind Data Context’ that we have created in Part 4 and inside this Data Context, we got Employees table and that is why I have used ‘NDC.Employees’.
Executing Query (using foreach loop)
One you done with query your next step will be executing it. Let’s go ahead and understand it too.
As we have bunch of rows/columns in ‘empQuery1’ or ‘empQuery2’ returned by the LINQ select statement, we need to specify which column we wanted to see in output and that’s why, I have used e.FirstName and e.LastName, where ‘e’ is instance of ‘empQuery1’ or ‘empQuery2’.
Alternatively, you can bind ‘empQuery1’ or ‘empQuery2’ directly to any Data Control like GridView.
In upcoming post, you will learn how to filter, order, group, and join using LINQ.