November 2006 - Posts

  • 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
  • XPO and LINQ - first steps

    The version of XPO included in our new DXperience 6.3 release includes our first shot at support for LINQ – unsupported and most probably buggy, I should add.

    To try it yourself, you must make sure you’re using the correct version of LINQ, or of System.Query.dll to be more precise. The version you need is included in the August 2006 CTP of ADO.NET vNext, and to install that, you’ll need to have the LINQ May 2006 CTP installed first. When creating a project, be sure to reference the System.Query.dll that is part of ADO.NET vNext – the GAC will still contain a reference to the LINQ CTP one, and the Visual Studio templates in the LINQ Preview category also reference the wrong version. There are templates in Visual Studio that you can use, in the category ADO.NET vNext CTP – whichever way you go, you should make sure that the referenced System.Query.dll is in the ADO.NET vNext CTP installation directory, by default at C:\Program Files\Microsoft SDKs\ADO.NET vNext CTP\Bin\System.Query.dll.

    Now, in addition to the standard assemblies required for XPO, you must add a reference to the assembly DevExpress.Xpo.v6.3.Query.dll. This can be found in the GAC, but it’s shown as DevExpress.Xpo.Linq, confusingly. There’s only one of those around, so once you find it, you should be fine :-)

    With all that in place, you should now be able to write code like this:

    using (UnitOfWork unitOfWork = new UnitOfWork()) {
      var albums = 
        from album in new XPQuery<Album>(unitOfWork)
          where album.Year == 1974
          select album;

    Let me repeat, this is currently in development. Feel free to let us know if you encounter really unexpected or inexplicable behaviour, but that is to be expected at this point. Have fun with this early preview!


Chat is one of the many ways you can contact members of the DevExpress Team.
We are available Monday-Friday between 7:30am and 4:30pm Pacific Time.

If you need additional product information, write to us at info@devexpress.com or call us at +1 (818) 844-3383


DevExpress engineers feature-complete Presentation Controls, IDE Productivity Tools, Business Application Frameworks, and Reporting Systems for Visual Studio, Delphi, HTML5 or iOS & Android development. Whether using WPF, ASP.NET, WinForms, HTML5 or Windows 10, DevExpress tools help you build and deliver your best in the shortest time possible.

Copyright © 1998-2018 Developer Express Inc.
All trademarks or registered trademarks are property of their respective owners