Exploring LINQ


During the old days of .net programming from framework 1.1 to framework 2.0, there's no LINQ library yet, so I established my skills on all sorts of different syntaxes for querying SQL, parsing XML Xpath, Object enumeration. But the demand for LINQ is unavoidable. This abstracted library for querying all sort of objects, be it SQL, dataset, XML, objects, arrays, etc. is quite handy because it allows you to query using a standard query syntax for all types of objects.

The linq syntax is easier to practice using this tool called linqpad (www.linqpad.com). This allows you to execute queries from specified database and tables. For instance I want to...

Read all data from table
from O in ORDERs select O

It has also support for reading only a portion and in the middle of table (similar to MySQL limit statement)

(from O in ORDERs select O).Skip(100).Take(50)

Joining two tables whose order id equals orderdetails id, and select firstname and the item from orderdetails.

from o in Orders join od in OrderDetails on o.ID equals od.OrderID select new {o.Firstname,od.Item}

Source: http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b

No comments:

Post a Comment