Pages






Monday, February 28, 2011

LINQ Tutorial with Dot Net Framework 3.0 and 3.5

With the advancement of technologies and database it has become a great challenge to retrieve data faster and with less burden on programming part.

It has become apparent that the next big challenge in programming technology is to cut the complexity of accessing and integrating information that is not natively defined using OO technology. The two most common sources of non-OO information are relational databases and XML.

The major advantage of Microsoft’s LINQ Project is the general approach they have taken because they have integrated a query language irrespective of any specific part.Language Integrated Query can be used with XML,Database or anything which is capable of returning IENUMERABLE.

Here is a small tutorial that can explaining. This is specially for my readers who love to do to programming.

This is a snippet which I found in the document itself which can express the power of LINQ and make programmers of dot net really smile.

using System; using System.Query;
using System.Collections.Generic;
class app {
static void Main() {
string[] names = { “Burke”, “Connor”, “Frank”,“Everett”, “Albert”, “George”,”Harris”, “David” };

IEnumerable expr = from s in names
where ( s.Length == 5 orderby s )
select s.ToUpper();
foreach (string item in expr)
Console.WriteLine(item);

}

}

How Linq works with XML ?

The extensibility of the query architecture is used in the LINQ project itself to give implementations that work over both XML and SQL data.

Xpath and Xquery which is used heavily is in the host programming language. Here is again a code snippet for showing how easy it is to work with LINQX(yes that’s what its called for XML’s)

The Standard way is done as follows:

XmlDocument doc = new XmlDocument();
XmlElement name = doc.CreateElement(“name”);
name.InnerText = “Patrick Hines”;
XmlElement phone1 = doc.CreateElement(“phone”);
phone1.SetAttribute(“type”, “home”);
XmlElement phone2 = doc.CreateElement(“phone”);
phone2.SetAttribute(“type”, “work”);
XmlElement street1 = doc.CreateElement(“street1″);
XmlElement city = doc.CreateElement(“city”);
city.InnerText = “Mercer Island”;
XmlElement state = doc.CreateElement(“state”);
state.InnerText = “WA”;
XmlElement postal = doc.CreateElement(“postal”);
postal.InnerText = “68042″;
XmlElement address = doc.CreateElement(“address”);
address.AppendChild(street1);
address.AppendChild(city);
address.AppendChild(state);
address.AppendChild(postal);
XmlElement contact = doc.CreateElement(“contact”);
contact.AppendChild(name);
contact.AppendChild(phone1);
contact.AppendChild(phone2);
contact.AppendChild(address);
XmlElement contacts = doc.CreateElement(“contacts”);
contacts.AppendChild(contact);
doc.AppendChild(contacts);

If we see it little closer the above code , This style of coding provides few clues to the structure of the XML tree. but with LINQX it becomes more of what they as functional construct.Here is what how u code the same with LINQX….

XElement contacts = new XElement
(“contacts”,new XElement(“contact”,
new XElement(“name”, “Patrick Hines”),
new XElement(“phone”, “206-555-0144″,
new XAttribute(“type”, “home”))
,new XElement(“phone”, “425-555-0145″,new XAttribute(“type”, “work”)),
new XElement(“address”,new XElement(“street1″, “123 Main t”),
new XElement(“city”, “Mercer Island”),
new XElement(“state”, “WA”),new XElement(“postal”,”6843″)

This is so simple no hassles at all.
How LINQ works with database

Yup its done By Microsoft again under LINQD(thats LINQ for database). The query operators over relational data (DLinq) build on the integration of SQL-based schema definitions into the CLR type system.

This integration provides strong typing over relational data while retaining the expressive power of the relational model and the performance of query evaluation directly in the underlying store.

What we kneed to know is the structure of database table we want to deal with it and create a class exactly having private variables as columns of the underlying table say may be like save that easy.Thus in simple way we create an object of the row of that table in memory and do operation on that and ask somebody to save it.Moreover we can ask for a collection of those rows fo get more than one record and do a query on it..isnt that great no hassles of database sql.

1. Create an entity class with mapping to database table like this:-

[Table(Name="Customers")]
public class Customer
{
[Column (Id=true)]
public string CustomerID;
private string _City;
[Column(Storage = "_City")]
public string City
{
get { return this._City; }
set { this._City = value; }}}

2.Create a Data context to load from database

static void Main(string[] args){
// Use a standard connection string
DataContext db = new DataContext(
@”C:Program FilesLINQ PreviewDatanorthwnd.mdf”);
// Get a typed table to run queries
Table Customers = db.GetTable();

3.Query what you get….

// Attach the log showing generated SQL to console
// This is only for debugging / understanding the working of DLinq
db.Log = Console.Out;
// Query for customers in London
var custs =
from c in Customers
where c.City == “London”
select c;
}

Thats how it works with database….easy isn’t it…This Technology will come with DotNet Framework 3.0 and with C#3.0 and VB 9. I havent covered every thing but just a quick glance which can get grasping things faster. If you want to read more on it see the Microsoft’s LINQ Project

SOURCE:http://www.technospot.net/blogs/linq-tutorial-with-dot-net-framework-30/

0 comments: