In simple terms, a query is an expression that retrieves
data from a data source.
Scope of this Article
1. Creating a LINQ project using
Visual Studio
2. Basics
3. LINQ Actions and some basic
examples
Creating a LINQ Project
As per MSDN,
To use LINQ to XML or LINQ to DataSet in either C# or
VB.Net language, you must manually add namespaces and references as described in
the following:
If you are upgrading a project that you created by using
an earlier version of Visual Studio, you may have to supply these or other LINQ
-related references manually and also manually set the project to target .NET
Framework version 3.5.
To target the .NET Framework version 3.5 in Visual
Studio 2008
In Visual Studio 2008, open a Visual Basic or C# project
that was created in Visual Studio 2005 and follow the prompts to convert it to a
Visual Studio 2008 project.
1. For a C# project, click the
Project menu, and then click Properties.
In the Application property
page, select .NET Framework 3.5 in the Target Framework drop-down list.
2. For a Visual Basic project,
click the Project menu, and then click Properties. In the Compile property page,
click Advanced Compile Options and then select .NET Framework 3.5 in the Target
Framework (all configurations) drop-down list.
Basic Operations of LINQ Query
LINQ query consists three distinct actions as defined
bellow:
(1) In this step, LINQ gather the DataSource, it may be
any source of data.
(2) In next step, have to create the Query.
(3) Finally, execute the Query.
Lest, go through the following code-lines and try to
understand the above operations:
class LINQQuery
{
static void Main()
{
// This is the Data source - Operation - I
int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6
};
// Query Creation - Operation - II
var numQuery =
from num in numbers
where (num % 2) == 0
select num;
// Query Execution - Operation - III
foreach (int num in numQuery)
{
Console.Write("{0,1} ", num);
}
}
}
|