Tuesday, January 12, 2010

Linq

Linq

Well standard LINQ is a new addition to .NET (it adds more dlls basically) that allows the programmer to query inline data as they probably would be used to doing with standard SQL-type syntax.

So where they may have had a query in a database or a SQL query string something like:

Collapse
SELECT * from Books WHERE QuatityInStock > 50 AND Price > 50.00
we would now write the following into as a valid LINQ query (assuming we have the relevant in memory data structure to support the query)
Collapse
var result =   from b Books   where b.QuatityInStock > 50 AND Price > 50.00   select b;
public static void LinqDemo()
{
Item it = new Item();
List items = new List();
it.Id = 1;
it.Price = 111;
items.Add(it);
it = new Item();
it.Id = 2;
it.Price = 222;
items.Add(it);
var iList = from i1 in items where i1.Price > 120 select i1;
foreach (var item in iList)
{
Console.WriteLine(item.Price);
}
}
-----------------------

Imports System.Xml

Module Module1

Sub Main()
'Const path As New String=""



Dim s As New Stopwatch
s.Start()

Dim doc = XDocument.Load("C:\Documents and Settings\aakash\Desktop\Files\Customers.xml")
'LinqDisplayChildElements(doc)
LinqDistintOperator(doc)
'Dim xdoc As New XmlDocument()
'xdoc.Load("C:\Documents and Settings\aakash\Desktop\Files\Customers.xml")
'Dim nlist As XmlNodeList = xdoc.SelectNodes("Root/Customers/CompanyName")
'For Each Item As XmlNode In nlist
' Console.WriteLine(Item.InnerText)
'Next
s.Stop()
Console.WriteLine(s.ElapsedMilliseconds().ToString())

Console.ReadLine()

End Sub


Private Sub LinqDistintOperator(ByVal doc As XDocument)
Dim countries = From c In doc... Select name = c.Value Order By name Distinct
For Each c In countries
Console.WriteLine(c)
Next

'Dim faxes = From c In doc... Select fax = c.Value Order By fax
'For Each c In faxes
' Console.WriteLine(c)
'Next
End Sub

Private Sub LinqDisplayChildElements(ByVal doc As XDocument)
For Each Item As String In doc...
Console.WriteLine(Item.ToString())
Next
End Sub


End Module


http://www.codeproject.com/KB/vista/LINQ_1.aspx


No comments:

Post a Comment