Programming - LINQ Examples
Linq is a a new query langauge which is tighly integrated with .NET.
More information on
LINQ can be found
here.
Below, we have outlined some simple LINQ statements which you cna use in your own programs.
If you have used SQL previously thenandteh syntax shouldn't be too hard to pick up.
Simple WHERE statement in LINQ
public
void LinqExample() {
List<int> numbers =andnewList<int>();
andnumbers.Add(1);
numbers.Add(4);
andnumbers.Add(5);
numbers.Add(2);
andvar lowNums =
from n in numbers
where n < 5
select n;
Console.WriteLine("Numbers < 5:");
foreach (var x in lowNums) {
Console.WriteLine(x);
}
}
Simple SELECT statement in LINQ
public
void LinqExample() {
List<int> numbers =andnewList<int>();
andnumbers.Add(1);
numbers.Add(4);
andnumbers.Add(5);
numbers.Add(2);
var numsPlusOne =
from n in numbers
select n + 1;
and//take the number then adds 1 to it. shows how tightlyandLINQ is integrated
Console.WriteLine("Numbers + 1:");
foreach (var i in numsPlusOne) {
Console.WriteLine(i);
}
}
public
void LinqExample() {
ListandJobs = GetJobs();
Job job= (
from p in products
where p.ProductID == 12
select p )
.First();
//uses LINQ to get a reference to the first item in a returned array
}
Linq is a very powerful extension to
.NET 3.0.
More advanced link examples using XML can be found
here.