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

Using LINQ

I tried to look for some examples on youtube on how to use LINQ and I found this video tutorial on how to use LINQ. It contain a code for C# and for VB. I believe this cool video is a good start for those who want to learn LINQ... Thanks to David Bush.














kick it on DotNetKicks.com

Friday, June 27, 2008

What is LINQ?

“Language Integrated Query”

Coming with C# 3.0 and VB.NET 9.0 with native language syntax for queries and provides class libraries to take advantage of these capabilities.

LINQ solves the impedance mismatch between the code that lives on a database server and the code we write with standard programming languages such as C# or VB.

Here are previous problems:
1. Worlds of Object Oriented and data access are far.
2. Object Oriented language types and native database types are often different
SQL statement is “inside the quotes”.
3. There is no strong typing or compile-time type checking.
4. Intellisense is not available.
5. SQL and XML have query languages, objects to not

LINQ seeks bridging the gap
Here are some of the benefits of LINQ:

DataContext
  • Strongly typed database connectionDesigner
  • Relationships
a. One-to-one, one-to-many
b. Inheritance
c. Custom types

Intellisense and compiler validation
  • “Strongly typed query”
  • Super easy to write query

Conclusion:

Easy to use
  • Very powerful for read operations
  • Absolutely recommended for 2-tier app
  • Good for most ASP.NET architectures
    • Where Data Access Layer and Presentation Layer are in the same process
The ugly side of LINQ is;
  1. Doesn't have multi tier change tracking support
  2. ADO.NET Entity Framework is an alternative
  3. Works only with SQL Server






Resources:
http://wm.microsoft.com/ms/msdn/visualcsharp/anders_2007_01/Anders_0001.wmv











kick it on DotNetKicks.com