Pages

Sunday, June 29, 2008

LinqToSql

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 http://blog.wekeroad.com/2007/12/14/aspnet-mvc-choosing-your-data-access-method/

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
select
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 : http://blog.wekeroad.com/2007/12/14/aspnet-mvc-choosing-your-data-access-method/
























kick it on DotNetKicks.com