LINQ – Yet another Masala Mix From Microsoft,Here after forget about writing SQL Queries

I have got the opportunity yesterday to watch the interview of Anders Hejlsberg, the chief architect of C# where described about the next release of C#, code named Orcas,It was quite interesting and amazing to hear about the “Orcas” features.

He also explained how 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.

Are you guys wondering what’s up in DLinq its a real gift for developers whom doesn’t have exposure to write SQL Queries,Creating Procedures and functions in SQL Server.

DLinq, a component of the LINQ Project, provides a run-time infrastructure for managing relational data as objects without giving up the ability to query. It does this by translating language-integrated queries into SQL for execution by the database and then translating the tabular results back into objects you define. Your application is then free to manipulate the objects while DLinq stays in the background tracking your changes automatically.

DLinq is designed to be non-intrusive to your application. It is possible to migrate current ADO.NET solutions to DLinq in a piecemeal fashion, sharing the same connections and transactions, since DLinq is simply another component in the ADO.NET family.

Creating Entity Classes
We will start with a simple class Customer and associate it with the customers table in the Northwind sample database. To do this, we need only apply a custom attribute to the top of the class declaration. DLinq defines the Table attribute for this purpose. 

[Table(Name=”Customers”)]
public class Customer
{
 public string CustomerID;
 public string City;
}
The Table attribute has a Name property that you can use to specify the exact name of the database table.  If no Name property is supplied DLinq will assume the database table has the same name as the class. Only instances of classes declared as tables will be able to be stored in the database. Instances of these types of classes are known as entities, and the classes themselves, entity classes.
In addition to associating classes to tables you will need to denote each field or property you intend to associate with a database column. For this, DLinq defines the Column attribute. 
[Table(Name=”Customers”)]
public class Customer
{
 [Column(Id=true)]
 public string CustomerID;
 [Column]
 public string City;
}
The Column attribute has a variety of properties you can use to customize the exact mapping between your fields and the database’s columns. One property of note is the Id property. It tells DLinq that the database column is part of the table’s primary key. 
As with the Table attribute, you only need to supply information in the Column attribute if it differs from what can be deduced from your field or property declaration. In this example, you need to tell DLinq that the CustomerID field is part of the table’s primary key yet you don’t have to specify the exact name or type.
Only fields and properties declared as columns will be persisted to or retrieved from the database. Others will be considered as transient parts of your application logic

 The DataContext
The DataContext is the main conduit by which you retrieve objects from the database and submit changes back. You use it in the same way that you would use an ADO.NET Connection. In fact, the DataContext is initialized with a connection or connection string you supply. The purpose of the DataContext is to translate your requests for objects into SQL queries made against the database and then assemble objects out of the results. The DataContext enables Language-Integrated Query by implementing the same operator pattern as the Standard Query Operators such as Where and Select.
For example, you can use the DataContext to retrieve customer objects whose city is London as follows:

// DataContext takes a connection string
DataContext db = new DataContext(“c:\\northwind\\northwnd.mdf”);
// Get a typed table to run queries
Table<Customer> Customers = db.GetTable<Customer>();
// Query for customers from London
var q =
 from c in Customers
 where c.City == “London”
 select c;
foreach (var cust in q)
 Console.WriteLine(“id = {0}, City = {1}”, cust.CustomerID, cust.City);
Each database table is represented as a Table collection accessible via the GetTable() method using its entity class to identify it. It is recommended that you declare a strongly typed DataContext instead of relying on the basic DataContext class and the GetTable() method. A strongly-typed DataContext declares all Table collections as members of the context.
public partial class Northwind : DataContext
{
 public Table<Customer> Customers;
 public Table<Order> Orders;
 public Northwind(string connection): base(connection) {}
}
The query for customers from London can then be expressed more simply as:
Northwind db = new Northwind(“c:\\northwind\\northwnd.mdf”);
var q =
 from c in db.Customers
 where c.City == “London”
 select c;
foreach (var cust in q)
 Console.WriteLine(“id = {0}, City = {1}”,cust.CustomerID, cust.City);

Click here to preview Orcas Language

 http://www.microsoft.com/downloads/details.aspx?familyid=1e902c21-340c-4d13-9f04-70eb5e3dceea&displaylang=en

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s