Language Integrated Query (LINQ)
is possibility the most exciting new feature Microsoft added that first
introduced in Visual Studio 2008 and .NET Framework version 3.5.
Language Integrated Query (LINQ) has its own capability in querying
a data from database, flexibility and overwhelming reach of LINQ to change the
way developers write application. Nevertheless, LINQ is a powerful strongly
typed syntax for accessing queries and generating results which improves
developer productivity to write less code that create a better application. For more information visit MSDN LINQ.
Actually, when I first explore
this language it looks weird and feels none – sense. Indeed, it helps saves a
lot of works. So far, I’m still learning about this language and publish it as
my own future reference at the same time providing relevant information for
those trying to learn LINQ. In this tutorial, it shows how to use queries using
LINQ.
Creating simple LINQ Queries using select, from, where, orderby, and join keywords.
First of all, I’ll show you a basic
LINQ query and for the next tutorial we’re going to work with databases queries. For now it’s simply a sort of a given array form.
string[] queryString = { "LINQ", "VB.NET",
"C#", "ASP.NET",
"C++" };
var str = from
strquery in queryString
select
strquery;
foreach (var data in str)
{
listBox1.Items.Add(data);
}
Output:
LINQ
VB.NET
C#
ASP.NET
C++
In this example it sorts
alphabetically with orderby keyword.
string[] queryString = { "LINQ", "VB.NET",
"C#", "ASP.NET",
"C++" };
var str = from
strquery in queryString
orderby
strquery
select
strquery;
foreach (var data in str)
{
listBox1.Items.Add(data);
}
Output:
ASP.NET
C#
C++
LINQ
VB.NET
In this example it shows only
specific result with where keyword.
string[] queryString = { "LINQ",
"VB.NET", "C#",
"ASP.NET", "C++" };
var str = from
strquery in queryString
where
strquery == "LINQ"
orderby
strquery
select
strquery;
foreach (var data in str)
{
listBox1.Items.Add(data);
}
Output:
LINQ
In this example it will display a number from 1 to
5
int[] number = { 1, 2, 3, 4, 5 };
var num = from
querynum in number
select
querynum;
foreach (var mynumber
in num)
{
listBox1.Items.Add(mynumber);
}
Output:
1
2
3
4
5
In this example use join keyword to join two arrays and
display the same element of both arrays.
int[] num1 = { 1, 2, 3, 4, 5 };
int[] num2 = { 1, 3, 7, 9, 5 };
var joinNum = from
queryNum1 in num1
join queryNum2 in
num2
on queryNum1 equals
queryNum2
select new
{queryNum1,queryNum2};
foreach (var myjoin in joinNum)
{
listBox1.Items.Add(myjoin);
}
Output:
{queryNum1 = 1, queryNum2 = 1}
{queryNum1 = 3, queryNum2 = 3}
{queryNum1 = 5, queryNum2 = 5}
When you noticed the output contains three array elements
1, 3, and 5 that compare num1 and num 2 Array. However there’s other way how to
construct join statement, instead with join, they use from, here is sample code.
var fromjoin = from queryA in arrayA
from queryB in arrayB
where queryA == queryB
select new { queryA,
queryB };
It will generate the same output as
what join statement stated.
If you are newbie using LINQ and
didn’t know yet, but it has already background either VB.NET or C#, don’t worry
because .NET 3.5+ has a build – in function that supports query syntax. Unfortunately,
lower version of .NET doesn’t support LINQ.
This comment has been removed by a blog administrator.
ReplyDeletethank to useful information
ReplyDelete