VB.NET GroupBy LINQ语句 [英] VB.NET GroupBy LINQ statement

查看:684
本文介绍了VB.NET GroupBy LINQ语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个时间让这个工作。我有一个 List(Of MyItem)名为 Items ,它们有一个 OrderId 属性。从这些项目中,我想创建一个 Order s的列表。一些项目将具有相同的 OrderId ,所以我想尝试按 OrderId 进行分组。然后我想按日期排序。这是我到目前为止:

 公共ReadOnly属性AllOrders()作为列表(订单)
获取
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

当然,这不会编译,并且出现错误:
$ b


类型为'System.Collections'的值。 Generic.List(Of System.Linq.IGrouping( Of Order,Order ))'不能转换为'System.Collections.Generic.List( Of Order ))'

我粗体显示了我认为问题所在的部分,但我不知道如何解决此问题。此外,它还用于在添加 .GroupBy 语句之前正常工作(除了有重复的值)。任何人有任何想法?



谢谢

编辑



基本上,我想要:

现有商品清单列表并将其转换为
清单MyItem):List(Of Order):
ItemId OrderId OrderID
1 100 100
2 102 102
3 100


解决方案

你不需要为此使用group by。

 公共ReadOnly属性AllOrders()以列表形式(订单)
获取
返回Items.Select Function(i)i.OrderID).Distinct.Select(Function(p)New Order(p))。ToList()
End
End Property

如果您想通过订单上的OrderedDate进行订购,只需在ToList之前添加一个orderby子句即可。

 公共ReadOnly属性AllOrders()As List(Of Order)
获取
返回Items.Select(Function(i)i.OrderID).Distinct _
.Select(Function(p)New Order(p))_
.OrderBy(Function(s)s.DateOrdered).ToList()
End
End Property


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

EDIT

Basically, I want this:

List of Existing Items      Take List and Turn it into 
List(Of MyItem):                 List(Of Order):
ItemId  OrderId                    OrderID 
1       100                        100
2       102                        102
3       100

解决方案

You don't need to use group by for this.

Public ReadOnly Property AllOrders() As List(Of Order)
Get
     Return Items.Select(Function(i) i.OrderID).Distinct.Select(Function(p) New Order(p)).ToList()
End Get
End Property

If you want to order it by the OrderedDate on the order, just add an orderby clause before the ToList

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

这篇关于VB.NET GroupBy LINQ语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆