Access:

» LINQ - C# 3.0

Related categories: XML | Windows | Object-oriented Technics | C#

Artur ¯arski
Viewed: 4602 | Article date: 2006-05-13 17:22:23

Programmers can work with such elements as classes, objects and methods. Looking at present and future generations of object-oriented languages, their main challenges are associated with integration of information which has not been created in object technology, such as relational or XML data. This article describes the latest solutions for C# and VB.NET like XLINQ (Language integrated query for XML) and DLINQ (Language integrated query for Data).

After twenty years, the industry has reached a stable point in evolution of object-oriented programming. Programmers can work with such elements as classes, objects and methods. Looking at present and future generations of object-oriented languages, their main challenges are associated with integration of information which has not been created in object technology, such as relational or XML data.

About the author

Artur ¯arski is a Developer Evangelist at Developer & Platform Group in Microsoft Company.

Contact with the author: arturz@microsoft.com

If one considers, relational or XML data, there is no simple mechanism which would make it possible to make objects out of this data and to work with it as if they were object data.

In order for one to be able to work simultaneously with relational/XML and object data, it is necessary to create some means of integration. For this purpose, the LINQ project has been initiated. The goal of this project is to create a new data integration language, called .NET Language Integrated Query (LINQ).

.NET Language Integrated Query defines a set of general proposals of operators, which will allow querying, filtering and projection of data. For querying relational data one uses DLINQ, that is - Data Language Integrated Query, whereas to work with XML data one uses XLINQ (XML Language Integrated Query). At present the LINQ project is available for C# and VB.NET. In the first case it implements the next version of the language, C# 3.0, in the second - of VB.NET 9.0

Let us consider the following fragment of code. Doesn't it look clear and easy to implement?

string[] wyrazy = {"jeden","dwa", "trzy", "cztery", "piec"};

var wynik =

from s in wyrazy

where s.Length == 5

select s;

wynik.Print();

What will be the result of its execution? This article is to present an overview of capabilities of LINQ along with its possible applications.

Going back to the question we have asked, the result will of course be the word "jeden". And now, what will happen if we replace "select s" with "select s.ToUpper()"? The result will be "JEDEN". This example, while perhaps not difficult, demonstrates the concept of language extensions. The beauty of LINQ is that the query can be directed everywhere: to a relational database, to an object, to a XML file.

Relational Data and DLINQ

.NET Language Integrated Query can be used to query relational data with no need to change existing syntax of the programming language. This functionality has been given a work name DLinq and makes use of integration of SQL schema with CLR metadata. This integration compiles SQL tables and definitions of views to CLR types, which can be available at the level of any language. DLinq defines two fundamental attributes, [Table] and [Column], which indicate which CLR types and properties correspond with outside data. The attribute [Table] can be used with classes and associated CLR types, with a named view or SQL table. The attribute [Column] can be used with any field or property and associated with a SQL column. Both attributes can be parametrised. An example definition of a table schema:

create table People (

Name nvarchar(32) primary key not null,

Age int not null,

CanCode bit not null )

create table Orders (

OrderID nvarchar(32) primary key not null,

Customer nvarchar(32) not null,

Amount int

)

The following code defines those SQL tables in Dlinq:

[Table(Name="People")]

public class Person {

[Column(DbType="nvarchar(32) not null", Id=true)]

public string Name;

[Column]

public int Age;

[Column]

public bool CanCode;

}

[Table(Name="Orders")]

public class Order {

[Column(DbType="nvarchar(32) not null", Id=true)]

public string OrderID;

[Column(DbType="nvarchar(32) not null")]

public string Customer;

[Column]

public int? Amount;

}

If we wanted to access data stored in a SQL database using presently-available solutions, we would have to do the following:

  • Create a connection and open it

SqlConnection c = new SqlConnection(…);

c.Open();

  • Create a query

SqlCommand cmd = new SqlCommand(

@"SELECT c.Name, c.Phone

FROM Customers c

WHERE c.City = @p0");

cmd.Parameters["@p0"] = "London";

  • Execute the query, then pass its result to a DataReader object

DataReader dr = c.Execute(cmd);

  • Read records one by one and work with them as needed. Finally, close the DataReader,

while (dr.Read()) {

string name = r.GetString(0);

string phone = r.GetString(1);

DateTime date = r.GetDateTime(2);

}

r.Close();

Using DLinq the same code would look like this:

  • Define a class corresponding with the structure of our SQL database

public class Customer { … }

public class Northwind: DataContext

{

public Table<Customer> Customers;

}

  • Create a connection

Northwind db = new Northwind(…);

  • Query the database.

var contacts =

from c in db.Customers

where c.City == "London"

select new { c.Name, c.Phone };

An important issue here is that our class with the database structure is created automatically, using LINQ tools. Of course if need be, it is possible to modify it.

A d v e r t i s e m e n t
Linux BSD Unix ranking vote

Page: 1 2
Buy article Buy subscription
Buy now add to cart
add to cart
Standard price: 2€/$3 Standard price: 25€/$30
Buy article for as little as (2€/$3) each allow access to individual articles. Buy a full access to our Software Developers's Journal archive portal. You will be able to read the articles from all archive issues from year 2005 and 2006. For just 25€/$30 you get unrestricted access to the entire website for the whole year.
SDJhakin9

.SDJ Users:


.:Login
.:Password

[Register]
[Forgotten your password?]

...Shopping Cart

sum: 0 €
Choose currency:

...Topics

...Advertisement

www.acunetix.com www.verifysoft.com

...Conferences




...Print Edition Archive

...Affiliate Program



 

 

Subscribe | Contact Us | Newsletter | Privacy policy | Regulations | See all issues | About SDJ
Copyright C 2006 by Software Developer's Journal. All rights reserved.