Search:
|
Access:
» LINQ - C# 3.0Related categories: XML | Windows | Object-oriented Technics | C# Artur ¯arskiViewed: 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 authorArtur ¯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:
SqlConnection c = new SqlConnection(…); c.Open();
SqlCommand cmd = new SqlCommand( @"SELECT c.Name, c.Phone FROM Customers c WHERE c.City = @p0"); cmd.Parameters["@p0"] = "London";
DataReader dr = c.Execute(cmd);
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:
public class Customer { … } public class Northwind: DataContext { public Table<Customer> Customers; … }
Northwind db = new Northwind(…);
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.
|
|
Copyright C 2006 by Software Developer's Journal. All rights reserved.






SDJ Users:
Shopping Cart









