LINQ QUerry : A Step Ahead Series
LINQ Query : A Step Ahead Series
History:
The article is related to previous posts :
1. Anonymous Types in LINQ: A Step Ahead Series
2. How to Test Anonymous Type Equality - LINQ: A Step Ahead Series
Introduction:
LINQ - Language Integreted Query is repeadily used in these days. The presented article is elaborate the idea "How to describe a LINQ Query".
Simply, a query is an expression that retrieves data from a data source.
Scope:
The scope of the presented article is upto its title and projected scenarios.
Description:
Now lets elaborate the topic in following points:
1. Operation of LINQ Query:
LINQ query consists three distinct actions as defined bellow:
(a) In this step LINQ gather the DataSource it may be any source of data.
(b) In next step have to create the Query
(c) 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);
}
}
}
Three actions of LINQ Query [adapted from MSDN - bb397906]
2. Skip and Take:
Skip, SkipWhile, Take, and TakeWhile are used to partition collectionsinto two parts and then return one of the parts. These partition features are only implemented as extension methods.
skip jumps to the indicated argument position in the sequence and returns everything after that.
SkipWhile accepts a predicate and instead of using a position, it
skips until the predicate evaluates to false.
Take grabs all of the elements up to the specified position in the index.
TakeWhile grabs all of the items in a sequence as long as the predicate evaluates to true.
3. Create 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
Important Points:
Filtering is the common operation tyhrough which you can filter the data, consider following code:
//filter all employees on last name as 'Arora'
var lnqueryNames = from empname in employee
where empname.lastname == "Arora"
select empname;
Ordering sets the ordering for the output, lets take a loon in the following:
//display all Arora in ascending order with departments
var lnqueryNames = from empname in employee
where empname.lastname == "Arora"
orderby empname.dept ascending
select empname;
Ordering give the output in a group, consider following example:
var lnqueryNames = from empname in employee
group empname by empname.department;
foreach (var empbydept in lnqueryNames)
{
Console.WriteLine(empbydept.Key);
foreach (Employee emp in empbydept)
{
Console.WriteLine(" {0}", emp.lastName);
}
}
HI,
Very good article, its having almost all detail.