This is fifth part of the "LINQ" series of articles that I have started from here. In the last article we dicussed how to setup a demo project to explore LINQ queries. I assume you have the demo project set up; if not then visit Part 4 of this series and set it up. Let's go ahead and explore selecting records using LINQ and explore internals.
Selecting data using LINQ queries is very simple. You will get full intellisense support while querying a database and even compile-time error checking that adds significant advantages to LINQ. Once you have learned it, you will love to use this in all your DB projects. I'm loving this, it is great.
In the image given below, you will find the sample code for selecting the employee's "FirstName" and "LastName" from the "Employee" table of the "Northwind" Database:
In the above image, you can see, I'm using the same queries two times, but there is a difference, don't go on output screen only. If you look at the code carefully, you will find I'm using IEnumerable<Employee> in the first block of code and just a "var" in the second block of code. Also note, for IEnumerable<Employee> I'm using "Employee" in the foreach loop and for "var" I'm using "var" in the 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 the following image (just hover the mouse over "empQuery1" when the compiler is on break). When you expand the index[], you will find all the 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 the from clause, no big difference; you still get all the benefits.
Now, let's discuss 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 is first, 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 an Employees table and that is why I have used "NDC.Employees".
Executing Query (using foreach loop)
When you are done with query your next step will be executing it. Let's go ahead and understand it too.
Since we have many rows and columns in "empQuery1" or "empQuery2" returned by the LINQ select statement, we need to specify which column we want to see in the output and that's why I have used e.FirstName and e.LastName, where "e" is the instance of "empQuery1" or "empQuery2".
Alternatively, you can bind "empQuery1" or "empQuery2" directly to any Data Control like GridView.
In an upcoming article, you will learn how to filter, order, group, and join using LINQ.
I hope you will find it useful. Thanks for reading.