views:

102

answers:

0

While playing with Linq Group By statements using both DataSet and Linq-to-Sql DataContext, I get different results with the following VB.NET 10 code:

#If IS_DS = True Then
    Dim myData = GetOrdersDS
#Else
    Dim myData = GetNwDataContext
#End If
    Dim MyList = From o In myData.Orders _
        Join od In myData.Order_Details On o.OrderID Equals od.OrderID _
        Join e In myData.Employees On o.EmployeeID Equals e.EmployeeID ) _
            Group By FullOrder = New With _
            { _
                 .OrderId = od.OrderID, _
                 .EmployeeName = (e.FirstName & " " & e.LastName), _
                 .ShipCountry = o.ShipCountry, _
                 .OrderDate = o.OrderDate _
            } _
            Into Amount = Sum(od.Quantity * od.UnitPrice) _
            Where FullOrder.ShipCountry = "Venezuela" _
            Order By FullOrder.OrderId _
            Select FullOrder.OrderId, _
                FullOrder.OrderDate, _
                FullOrder.EmployeeName, _
                Amount

    For Each x In MyList _
        Console.WriteLine( _
         String.Format( _
          "{0}; {1:d}; {2}: {3:c}", _
          x.OrderId, _
          x.OrderDate, _
          x.EmployeeName, _ 
          x.Amount))
    Next

With Linq-to-SQL, the grouping works properly, however, the DataSet code doesn't group properly. Both the DataSet and DataContext were created in the usual Draggy-Droppy way that Microsoft loves to use in demonstrations.

Here are the functions that I call to create the DataSet and Linq-to-Sql DataContext

Private Function GetOrdersDS() As NorthwindDS
    Dim ds As New NorthwindDS
    Dim ota As New OrdersTableAdapter
    ota.Fill(ds.Orders)
    Dim otda As New Order_DetailsTableAdapter
    otda.Fill(ds.Order_Details)
    Dim eda As New EmployeesTableAdapter
    eda.Fill(ds.Employees)
    Return ds
End Function
Private Function GetNwDataContext() As NorthwindL2SDataContext
    Dim s As New My.MySettings
    Return New NorthwindL2SDataContext(s.NorthwindConnectionString)
End Function

What am I missing? If it should work, how do I make it work, if it can't work, why not (what interface isn't implemented, etc)?

ADDITIONAL: (2010-03-22) I have noticed that if I call AsEnumerable on Orders when using Linq-to-Sql, I get the same bad behavior as the DataSet.

Dim MyList = From o In myData.Orders.AsEnumerable ...

It does work properly if I call AsEnumerable on Order_Details or Employees. My guess is that some method in some interface is not implemented the way GroupBy likes it. But I don't know which one.