LINQ Experimental Support

XPO Team Blog
27 November 2006
Well, Oliver has taken the lead over me announcing the LINQ support in his message. Hopefully, the community will appreciate some additional information on this subject from me.

The XPO 2006.3 release includes a new assembly - DevExpress.Xpo.v6.3.Query. This assembly implements the XPQuery class for constructing LINQ expressions against persistent objects. To run the attached sample project, you should have Visual Studio 2005, SQL Server 2005, XPO v6.3, LINQ Preview (May 2006) and ADO.NET vNext CTP (August 2006) installed. Below are the sample expressions from our project.

// simple Select with Where and OrderBy clauses
XPQuery<Customer> customers = new XPQuery<Customer>(Session.DefaultSession);
XPQuery<Order> orders = new XPQuery<Order>(Session.DefaultSession);
XPQuery<Employee> employees = new XPQuery<Employee>(Session.DefaultSession);

var list = from c in customers
           where c.Country == "Germany"
           where c.ContactTitle == "Sales Representative"
           orderby c.ContactName
           select c;
foreach(Customer cust in list)
    Console.WriteLine("{0}\t{1}\t{2}", cust.ContactName, cust.Country, cust.ContactTitle);
...
// Select Top 5 objects
var list = (from o in orders
            orderby o.ShippedDate descending
            select o).Take(5);
foreach(Order order in list)
    Console.WriteLine("{0}\t{1}", order.OrderID, order.ShippedDate);
...
// Join customers with an aggregation on their Orders
var list = from c in customers
           join o in orders on c equals o.Customer into oo
           where oo.Count() >= 1
           select new { c.CompanyName, OrderCount = oo.Count() };
foreach(var item in list)
    Console.WriteLine("{0}\t{1}", item.CompanyName, item.OrderCount);
...
// an example of aggregated functions (Count and Average)
var list = from o in orders
           select o;
int count = list.Count();
Console.WriteLine("Orders Row Count: {0}", count));

decimal avg = list.Average(x => x.Freight);
Console.WriteLine("Orders Average Freight: {0:c2}", avg);
...
// Select with Group By
var list = from c in customers
           group c by c.ContactTitle into cc
           where cc.Count() >= 1
           select new { Title = cc.Key, Count = cc.Count()};
foreach(var item in list)
    Console.WriteLine("{0}\t{1}", item.Title, item.Count);
...
// Any method 
bool result = customers.Any(c => c.Country == "Spain");
Console.WriteLine("Is there any customer from Spain? {0}", result ? "Yes" : "No");
result = customers.Any(c => c.Country == "Monaco");
Console.WriteLine("Is there any customer from Monaco? {0}", result ? "Yes" : "No");


Please pay attention to the fact that XPO LINQ expressions are turned into pure database queries. That is, an expression is processed on the database server's side and only the requested objects (or a scalar value) are loaded onto the client. To see all of this in action, take a close look at the database queries executed by XPO (displayed in a log window in our sample project).

There are more LINQ methods which are already supported by XPO, but haven't been demonstrated in the attached sample. Such methods include All, First, FirstOrDefault, Union, EqualAll, etc. We are open to suggestions and bug reports as we move forward with improved LINQ support. Do note, however, that the current status of this support is "experimental" and LINQ itself is only in CTP stage.
The Attachment

Free DevExpress Products - Get Your Copy Today

The following free DevExpress product offers remain available. Should you have any questions about the free offers below, please submit a ticket via the DevExpress Support Center at your convenience. We'll be happy to follow-up.
Tags
No Comments

Please login or register to post comments.