Sunday, June 29, 2008


Today I tried to used the LinqToSql in my project. In order to use LinqToSql, you have to learn LINQ. Here are the sample code to appreciate the benefits of LinqToSql, by the way, big thanks to Rob Conery for this example. Here is his blog

NorthwindDataContext db=new NorthwindDataContext ();

var result = from p in db.Products
select p;

And to returns an IQueryable object.

foreach(var p in result){
//do something...

Data in List<> form

List<Northwind.Product> list=p.ToList<Northwind.Product>();

This sample in much complex but it is still readable :)

var customerOrderGroups = 
from c in customers
new {c.CompanyName,
YearGroups =from o in c.Orders
group o by o.OrderDate.Year into yg
select new {Year = yg.Key,
MonthGroups = from o in yg
group o by o.OrderDate.Month into mg
select new { Month = mg.Key, Orders = mg }

Reference Url :

kick it on