Just another tech site



Language INtegrated Query




The DataContext is the main conduit by which you retrieve objects from the database and resubmit changes.

Different LINQ


// DataContext takes a connection string
DataContext db = new   DataContext("c:\\northwind\\northwnd.mdf");
// Get a typed table to run queries
Table Customers = db.GetTable();
// 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);

Stronged type DataContext


public partial class Northwind : DataContext
   public Table Customers;
   public Table Orders;
   public Northwind(string connection): base(connection) {}

LINQ to Entity

Delaration of an Entity

public class Customer
   public string CustomerID;
   public string City;


Reading an XML file with LinQ

The XML file structure

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 St"),
                new XElement("City", "Mercer Island"),
                new XElement("State", "WA"),
                new XElement("Postal", "68042")

The code

string filename = Server.MapPath("~") + "/Path/FileName.xml";
// Create the query 
var custs =
    from c in
    select c;

// Execute the query 
foreach (var processTipo in custs)
    string name = (string) processTipo.Element("name");
    string notiz = (string) processTipo.Element("notiz");
    string enable_flag = (string) processTipo.Element("enable_flag");
    string table_name = (string) processTipo.Element("table_name");
    string detail_url = (string) processTipo.Element("detail_url");
    string code = (string) processTipo.Element("code");

    IEnumerable elList =
        from el in processTipo.Element("ProcessStateList").Descendants("Process_State")
        select el;

    foreach (XElement ProcessState in elList)
        string ps_name = (string) ProcessState.Element("name");
        string ps_description = (string) ProcessState.Element("description");
        string ps_icon = (string) ProcessState.Element("icon");
        string ps_flag_begin = (string) ProcessState.Element("flag_begin");
        string ps_process_tipo_id = (string) ProcessState.Element("process_tipo_id");
        string ps_code = (string) ProcessState.Element("code");



1.- Less code
2.- The keyword are validated at compilation
3.- LINQ makes abstraction of the data provider.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

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

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s

Tag Cloud

%d bloggers like this: