LINQ is not just for the developers who used to write queries for databases but also for the Functional Programmers. So do not worry if you are not very comfortable with SQL kind of queries, we have a very nice option called “Lambda Expression”. Here I am going to demonstrate the scenario for both the options with a small example. This example has an array of integers and I am only retrieving the even numbers using the power of LINQ. Here we go
using System;
using System.Collections.Generic;
using System.Text;
using System.Query;
using System.Xml.XLinq;
using System.Data.DLinq;
namespace LINQConsoleApplication1
{
class Program
static void Main(string[] args)
int[] arrInt = {1,2,3,4,5,6,7,8,9,10};
#region Place to change
//Language Integrated Query
var aa = from s in arrInt
where s % 2 == 0
select s;
#endregion
foreach (var item in aa)
Console.WriteLine("{0}", item);
}
Console.ReadKey();
If you do not want to use the different approach of query for Language then you are free to use Lambda Expression. So just replace the #region area with the following code block results will be identical.
//Lambda Expression
var aa = arrInt.Where(s => s % 2 == 0);
Output will look like
Output
=====================
2
4
6
8
10
Namoskar
This only works in .NET 3.0 right?
This works with .NET 2.0 also. You need to install LINQ for VS 2005 (refer http://blogs.msdn.com/wriju/archive/2006/09/14/753285.aspx) .But LINQ is targeted for .NET Framework 3.5 and Orcas (Visual Studio 2007).
Wriju
Interesting discussion on this topic
http://programming.reddit.com/info/tzu7/comments
It does not only work in .NET 3.0. It works also in .NET 3.5
@wriju : Visual Studio 2007? really? must have slipped past me, that one. :-)
@stimul8d, Bang on.. It should be Visual Studio 2008 :)
Hi ,
can you provide the lambda expression for this
int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 };
int[] numbersB = { 1, 3, 5, 7, 8 };
var pairs =
from a in numbersA
from b in numbersB
where a < b
select new { a, b };