in
Forums
Blogs
Files
Devexpress.Com
ClientCenter
Support Center
DevExpress Channel

XPO

eXpress Persistent Objects

LINQ Experimental Support

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
Published Nov 27 2006, 06:34 AM by Nick (Developer Express)
Filed under:
Technorati tags: XPO

Comments

 

Ben Hayat said:

Nick, can you explain what parts of LINQ is being used (internally) within XPO? Are you basically replacing the XPO filtering with LINQ select statement?  And what about add/update/delete? Are those XPO's original system or LINQ's?

I think in a nutshell can you explain how LINQ and XPO compliment each other and where the overlappings are?

Thanks!
November 30, 2006 5:00 PM
 

Nick (Developer Express) said:

Hello Ben,

Thank you for your questions.

>Nick, can you explain what parts of LINQ is being used (internally) within XPO?
XPO supports LINQ via the XPQuery class, which is in the DevExpress.Xpo.v6.3.Query assembly.  DevExpress.Xpo.v6.3.Query refers to the System.Query assembly and imports the IOrderedQueryable and IQueryable interfaces from it.

How it works: You write an expression in the LINQ syntax against an XPQuery object.  This gives you an IEnumerable object.  When an enumeration (a foreach loop) is started against the expression, the XPQuery parses the expression, converts it into XPO's CriteriaOperator and calls either the Session.GetObjects or Session.Evaluate method to pull out the elements.  Each element is either a persistent object (e.g. the Customer object in the first example listed above) or an anonymous object, which is dynamically made up of the queried fields (e.g. the Join example).

>Are you basically replacing the XPO filtering with LINQ select statement?
No, we aren't.

>And what about add/update/delete? Are those XPO's original system or LINQ's?
LINQ is not involved in XPO's add/update/delete operations.

Thank you,
Nick
December 3, 2006 7:52 AM
 

David Shannon said:

I was completely baffled by the sample above until I realized that where it has the following lines:

XPQuery customers = new XPQuery(Session.DefaultSession);  
XPQuery orders = new XPQuery(Session.DefaultSession);  
XPQuery employees = new XPQuery(Session.DefaultSession);  

your sample code reads like this:

XPQuery<Customer> customers = new XPQuery<Customer>(Session.DefaultSession);
XPQuery<Order> orders = new XPQuery<Order>(Session.DefaultSession);
XPQuery<Employee> employees = new XPQuery<Employee>(Session.DefaultSession);

I was beginning to think that LINQ was code for "psychic"... :)

Is XPQuery usable in place of XPCollection as a datasource for xtragrid?

Thanks,

David Shannon
December 3, 2006 11:47 PM
 

Nick (Developer Express) said:

Hi David!

Thank you for your comment.  I was not aware that our blog site did not escape the < and > symbols in code blocks.  I've replaced them with "&lt;" and "&gt;" and now the code sample looks plausible.

>Is XPQuery usable in place of XPCollection as a datasource for xtragrid?
No, it isn't.  We will add a method or constructor to the XPCollection class, which populates the collection from an IEnumerable object.  It will allow you to fill the XPCollection with the LINQ expression result and bind it to a data grid.

Thank you,
Nick
December 4, 2006 2:25 AM
 

Dan Vanderboom said:

Thanks for the LINQ support, it's really going to expand on the power of XPO!
December 4, 2006 10:41 AM
 

Righardt Marais said:

Any progress on moving this forward now that VS 2008 has been released?  Is the support still only experimental?

November 26, 2007 5:45 AM
 

Nick (Developer Express) said:

We'll announce XPO LINQ support within a week.  It will be included in the next maintenance update of DXperience 2007.3 - v7.3.4.

November 27, 2007 1:00 AM
 

René Weibel said:

DXperience v7.3.4 is now released, but there is no documentation about LINQ support.

December 4, 2007 8:45 AM

Leave a Comment

(required)  
(optional)
(required)  
Verification code: Required
   
Add
Copyright © 1998-2008 Developer Express Inc.
ALL RIGHTS RESERVED