VB.NET GroupBy LINQ statement

Posted by Jason on Stack Overflow See other posts from Stack Overflow or by Jason
Published on 2010-05-14T04:26:00Z Indexed on 2010/05/14 4:34 UTC
Read the original article Hit count: 510

Filed under:
|
|

I am having a bear of a time getting this to work. I have a List(Of MyItem) called Items that have a OrderId property on them. From those items, I want to create a list of Orders. Some items will have the same OrderId so I want to try to group by OrderId. I then want to sort by date. Here's what I have so far:

Public ReadOnly Property AllOrders() As List(Of Order)
    Get
         Return Items.Select(Function(i As MyItem) New Order(i.OrderID)) _
         .GroupBy(Function(o As Order) New Order(o.OrderID)) _
         .OrderBy(Function(o As Order) o.DateOrdered).ToList
    End Get
End Property

This, of course, doesn't compile, and I get the error:

Value of type 'System.Collections.Generic.List(Of System.Linq.IGrouping(Of Order, Order))' cannot be converted to 'System.Collections.Generic.List(Of Order))'

I've bolded the part where I think the problem is, but I have no idea how to fix it. Also, it used to work fine (except there were duplicate values) before I added the .GroupBy statement. Anyone have any ideas?

Thanks

© Stack Overflow or respective owner

Related posts about vb.net

Related posts about LINQ