What is LINQ? Explaination with a Demo
You all know that .NET 3.5 Framework is an incremental release of the .NET Framework and its formed by adding LINQ, ASP.NET 3.5, CLR Add-in Framework and several other.
The .NET Framework 3.5 builds upon the previous versions of the framework, namely the .NET Framework 2.0 and 3.0. More specifically, you can think of it as though the .NET Framework 3.5 has a dependency on the .NET Framework 3.0 with SP1 and 2.0 with SP1.
What is LINQ??
LINQ stands for Language Integrated Query which means the querying capabilities are built into the language. You can do queries against pretty much any kind of data, not just collections. It’s the newest Technology that’s integrated into the newest versions of C# AND VB.NET dealing with organizing your data structures in a manner similar to the way databases organizes data.
It’s not just an enhancement to the collection classes. Instead, it’s built right into the language.
How do I work on it??
1. Visual Studio 2008 to be installed in your system.
In order to program with LINQ, you need the latest C# or VB.NET. The best place to get these is Visual Studio 2008 (codenamed Orcas).
2. Don’t want Visual Studio 2008 to be installed.
You can instead just install the .NET 3.5 framework and can compile C# and VB.NET code at the command line. This is relatively easy to do, but you need your own editor and need to write all the code from scratch without the help of project templates. which is bit difficult.
Demo
We are going to develop a console application in VS 2008. In this program we are creating a function named divisible () which selects and prints the number that are divisible by 2.
These are the steps to be followed
1. All you need to do is create a new project and open a console application in Visual Studio 2008.
2. Within the class program create a void function, divisible ().
3. Create and initialize an array of integers, arr [].
4. Right after that is a bolded and enlarged expression below, which may look a bit odd. It looks like an SQL query! The code is an expression that says to obtain all the numbers in the array that are divisible by 2 and store the results in a variable n.
5. Create a foreach loop for finding x in Nums and print it.
6. Call the function divisible() from main.
Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace linq_demo
{
class Program
{
public static void divisible()
{
int[] arr = { 5,10,15,20,35};
var Nums=
from n in arr
where n%2==0
select n;
foreach (var x in Nums)
{
Console.WriteLine(x);
}
}
static void Main(string[] args)
{
divisible();
Console.ReadLine();
}
}
}
The output is
10
20
This, as you can see, is a list of all the numbers in the array that are divisible by 2.
Look at the bolded and enlarged expression above, it has three parts:
1. A from clause
The from clause says where to get the data.
2. A where clause
The where clause specifies a restriction on which members of the data that we want.
3. A select clause.
The final clause grabs the data
Notice the data type of the Nums variable: we're using a datatype called var, which means the compiler will figure out (at compile time) what type to make for the variable. When programming with LINQ this helps simplify matters. However, clearly Nums must have a data type. In fact, it's an internal type that implements IEnumerable
IEnumerable
from n in numbers
where n %2==0
select n;
But it would be easier if you simply declare it as var , you need not find out the exact data type and then use.This helps simplify matters.
Comments
I am working for a software integrator company. My projects includes working on Java and Ruby on Rails and Ajax. I think Web Services is really cool. We also recently have to now work on REST and they are talking about mashups and Struts. Can anyone tell me if there are some good training or conferences so that me and my team members can get to speed with these technologies. Learning from books is not my cup of tea, even not when I was doing engineering ;)
All the help that group members can provide in this regard is much appreciated.
Thanks,
Vaibhavi