Linq: Why won't Group By work when Querying DataSets?

Posted by jrcs3 on Stack Overflow See other posts from Stack Overflow or by jrcs3
Published on 2010-03-20T02:01:38Z Indexed on 2010/03/20 2:11 UTC
Read the original article Hit count: 285

Filed under:
|
|

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 = VbDataUtil.getOrdersDS
#Else
    Dim myData = VbDataUtil.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 Linq2SQL, the grouping works properly, however, the DataSet code doesn't group properly.

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

Public Shared 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
Public Shared 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)?

© Stack Overflow or respective owner

Related posts about vb.net

Related posts about LINQ