ArtinSoft's Blogs

Software Migration Experts
Welcome to ArtinSoft's Blogs Sign in | Join | Help
in Search

Mauricio Rojas Blog

LINQ Project

These project extends the VB and C# languages with query, set and transforms operations. It adds a native syntax for those operations.

 

The idea of the LINQ project is to make data manipulation part of the language constructs. Lets see these VB examples for LINQ:

 

The following examples associates the Customer class to the Customer table. Just adding the Column Tag before a field, maps it to a table column.

 

    <Table(Name:="Customers")> _

    Public Class Customer

        <Column(Id:=True)> _

        Public CustomerID As String

       

        <Column()> _

        Public City As String

       

    End Class

To access the database you do something like:

' DataContext takes a connection string

Dim db As DataContext = _
        New DataContext("c:\...\northwnd.mdf")

      'Get a typed table to run queries

      Dim Customers As Table(Of Customer) = db.GetTable(Of Customer)()

      'Query for customers from London

        Dim q = _

          From c In Customers _

          Where c.City = "London" _

          Select c

 

      For Each cust In q

          Console.WriteLine("id=" & Customer.CustomerID & _

              ", City=" & Customer.City)

      Next

 

You just create a DataContext and create typed object that will relate dot the relational tables. I think this is awesome!!

 

It is even nicer if you create a strongly typed DataContext

 

    Partial Public Class Northwind

        Inherits DataContext

        Public Customers As Table(Of Customer)

        Public Orders as Table(Of Order)

        Public Sub New(connection As String)

            MyBase.New(connection)

 

Your code gets cleaner like the following:

    Dim db As New Northwind("c:\...\northwnd.mdf")

 

    Dim q = _

        From c In db.Customers _

       Where c.City = "London" _

        Select c

     

      For Each cust In q

          Console.WriteLine("id=" & Customer.CustomerID & _

              ", City=" & Customer.City)

      Next

 

 

These project will start a lot of exciting posibilities. I recommed you have a look at’: http://msdn.microsoft.com/data/ref/linq/

 

 

 

Published Dec 31 2006, 12:10 PM by Mrojas
Filed under:

Comments

No Comments
Powered by Community Server (Non-Commercial Edition), by Telligent Systems