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
Anonymous
Ben Hayat
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!
30 November 2006
Nick (DevExpress Support)
Nick (DevExpress Support)
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
3 December 2006
Anonymous
David Shannon
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
3 December 2006
Nick (DevExpress Support)
Nick (DevExpress Support)
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
4 December 2006
Anonymous
Dan Vanderboom
Thanks for the LINQ support, it's really going to expand on the power of XPO!
4 December 2006
Righardt Marais
Righardt Marais

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

26 November 2007
Nick (DevExpress Support)
Nick (DevExpress Support)

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.

27 November 2007
Marco G.
Marco G.

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

4 December 2007
Saut Napitupulu
Saut Napitupulu

can you give example how to update and delete using linq to xpo?

27 September 2013
Brinashmita
Brinashmita

XPQuery<Customer> customers = new XPQuery<Customer>(Session.DefaultSession);

in this cutomers return null

what is problem in this??

31 October 2017
Brinashmita
Brinashmita

XPQuery<Customer> customers = new XPQuery<Customer>(Session.DefaultSession);

in this cutomers return null

what is problem in this??

31 October 2017
Brinashmita
Brinashmita

XPQuery<Customer> customers = new XPQuery<Customer>(Session.DefaultSession);

in this cutomers return null

what is problem in this??

31 October 2017
Brinashmita
Brinashmita

XPQuery<Customer> customers = new XPQuery<Customer>(Session.DefaultSession);

in this cutomers return null

what is problem in this??

31 October 2017
Brinashmita
Brinashmita

XPQuery<Customer> customers = new XPQuery<Customer>(Session.DefaultSession);

in this cutomers return null

what is problem in this??

31 October 2017
Brinashmita
Brinashmita

XPQuery<Customer> customers = new XPQuery<Customer>(Session.DefaultSession);

in this cutomers return null

what is problem in this??

31 October 2017

Please login or register to post comments.