Language Integrated Query (Linq) : Creating A LINQ Project
Language Integrated Query (Linq) : Creating A LINQ Project
Language Integrated Query (Linq) : Creating A LINQ Project
Language-Integrated Query (LINQ) can be described as one the BEST features available with Visual Studio
2008 and .NET 3.5.
LINQ helps you to work with Data as Objects and Query, Access, Modify and Manipulate them. With Visual Studio
2008 we can write queries in C# or VB.NET. You can query against any object which can also be a SQL Server
Database or XML documents or ADO.NET datasets or any collection of objects that supports IEnumerable or
IEnumerable interfaces.
This blog post deals on how to get started with LINQ with Visual Studio 2008 and C#.
We now have a new data type var where the compiler determines the type that is associated and assigns the
most appropriate when the query result is returned.
In our above example, the compiler determines that the type of the strSplit to be String and assigns it. Please
understand that the keyword var doesn’t mean that it is loosely typed.
Here our array is nothing but an IEnumerable object which allows us to query against them and return the value
in the variable query. If you check the type of the variable query, you will see that it’s nothing but of type
IEnumerable which confirms us that the query selects all the names in the array of type String. You can check the
type by,
Query Execution
The above query doesn’t result in the execution, instead the query variable stores the actual query commands
and it gets executed until it’s used or really needed. This is called Deferred Execution.
When we say that query doesn’t get executed, then what does the variable query holds? – It holds an object that
can give values that we would want to have
So, the above query will be executed only when we start iterating the foreach loop, which is shown below,
But there are also sometimes that the query has to be executed immediately and one such example is,
In the above example, the query has to be executed to get the count of elements. Similarly if you want to force
immediate query and cache those results, we can do something like,
The above query is forced to execute immediately and cache the results in a local variable query
Moving Forward
Our first query didn’t do much other than just returning all the names. Let us go to another utility function which
filters the array of names by a Name
IEnumerable[string] query =
from name in names
where name == matchName
select name;
Now we have our Where clause where our condition is specified and the rest looks same as in our first query. One
thing to note in the above query is that we have explicitly specified the type of our query result to
be IEnumerable[string] instead of var. So, LINQ also allows specifying query return types explicitly.
IEnumerable[string] query =
from name in names
where name == matchName
select name;
The only difference is that we have used method-based query syntax. The method-based syntax is defined
as Extension Methods of the type they operate on and they also make use of Lambda Expressions.
var query =
from name in names
select new
{
firstPart = name[0],
lastPart = name.Substring(1, (name.Length - 1))
};
We define two anonymous types - firstPart and lastPart and when we query now, we get,
let Clause
Sometimes it becomes a necessity to store the result of another sub-expression in a query and use it later.
We can do this with the let keyword, which creates a new range variable and initializes it with the result of that
sub-expression we specify. Let us re-write the above query which we used for anonymous types to use let
keyword,
var query =
from name in names
let firstPart = name[0]
let lastPart = name.Substring(1, (name.Length - 1))
select new { firstPart, lastPart };