locked
Compiler Fails to Substitute SelectMany for Select on Translation of Query Expression in Orcas June 2007 CTP

    Question

  • This might be cockpit error on my part, but I'm encountering errors when running C# or VB query expressions that I understand the language compiler should translate to the SelectMany standard query operator. In the following code examples, the CustomerList source collection contains an Orders field of a List(Of Order) type (per the customers collection of the "Standard Query Operators" white paper (June 2006), and txtResult is a multi-line text box.

     

    [Minor edit and removal of incorrect error message 7/24/2007 0630 PDT]

     

    The commented query expression of the following C# snippet won't compile with conflicting query above commented. The error occurs in the iterator (can't find the property values):

     

    Code Snippet

    var query6 = CustomerList.Where(c => c.Country == "USA")

    .SelectMany(c => c.Orders);

     

    /* The following won't compile

    var query6 = from c in CustomerList

    where c.Country == "USA"

    select c.Orders;

     

    foreach (var o in query6)

    txtResult.Text += String.Format("CustomerID = {0}, CompanyName = {1}, OrderID = {2}\r\n",

    o.CustomerID, o.Customer.CompanyName, o.OrderID);

     

    The commented query expression of the following VB snippet won't compile for the same reason:

     

    Code Snippet

    Dim Query6 = CustomerList.Where(Function(ByVal c) c.Country = "USA") _

    .SelectMany(Function(ByVal c) c.Orders)

     

    ' The following won't compile

    ' Dim Query6 = From c In CustomerList _

    ' Where c.Country = "USA" _

    ' Select c.Orders

     

    For Each o In Query6

    txtResult.Text &= String.Format("CustomerID = {0}, CompanyName = {1}, OrderID = {2}" + vbCrLf, _

    o.CustomerID, o.Customer.CompanyName, o.OrderID)

    Next

     

    In both cases, IntelliSense includes Orders as a valid property of o or c. However, the two query expressions return List<Order> and List(Of Order) rather than the expected IEnumerable<Order>/IEnumerable(Of Order) expected from a transalation of the Select to SelectMany operator.

     

    Both method call syntax queries behave as expected and return identical results.

     

    If this is a known problem with Orcas June 2007 CTP, will it be fixed in Beta 2? If it's my cockpit error, I'd appreciate learning what's wrong with the code.

     

    Thanks in advance,

     

    --rj

     

     

     

    Tuesday, July 24, 2007 12:34 AM

Answers

  • Using 'select' does not translate to SelectMany being called. A nested 'from' is translated to SelectMany.

     

    from c in CustomerList

    where c.Country == "USA"

    from o in c.Orders

    select o

     

    If you select a collection (c.Orders), you will be producing a sequence of collections, making your foreach variable 'o' List<Order>.

     

    Tuesday, July 24, 2007 4:11 PM

All replies

  • Using 'select' does not translate to SelectMany being called. A nested 'from' is translated to SelectMany.

     

    from c in CustomerList

    where c.Country == "USA"

    from o in c.Orders

    select o

     

    If you select a collection (c.Orders), you will be producing a sequence of collections, making your foreach variable 'o' List<Order>.

     

    Tuesday, July 24, 2007 4:11 PM
  • Matt,

     

    Thanks for clearing that up.

     

    On the other hand, IntelliSense might need a fixup if practical.

     

    --rj

     

    Tuesday, July 24, 2007 8:22 PM