获取“价值” IGrouping 中的属性
我有一个像这样的数据结构
public DespatchGroup(DateTime despatchDate, List<Products> products);
,我正在尝试做...
var list = new List<DespatchGroup>();
foreach (var group in dc.GetDespatchedProducts().GroupBy(i => i.DespatchDate))
{
// group.Values is not correct... how do I write this?
list.Add(new DespatchGroup(group.Key, group.Values);
}
我显然不理解IGrouping
,因为我看不到如何实际获取组内的数据记录!
I have a data structure like
public DespatchGroup(DateTime despatchDate, List<Products> products);
And I am trying to do...
var list = new List<DespatchGroup>();
foreach (var group in dc.GetDespatchedProducts().GroupBy(i => i.DespatchDate))
{
// group.Values is not correct... how do I write this?
list.Add(new DespatchGroup(group.Key, group.Values);
}
I'm obviously not understanding IGrouping
as I can't see how to actually get to the data records within the group!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
该组实现
IEnumerable
- 在一般情况下,只需在group
上调用foreach
即可。在这种情况下,因为您需要一个List
:The group implements
IEnumerable<T>
- In the general case, just callforeach
over thegroup
. In this case, since you need aList<T>
:没有
Values
属性或类似属性,因为IGrouping
本身是IEnumerable
值序列。在这种情况下,您需要做的就是将该序列转换为列表:There's no
Values
property or similar because theIGrouping<T>
itself is theIEnumerable<T>
sequence of values. All you need to do in this case is convert that sequence to a list:对于任何选定的组,您可以致电
For any selected group,you could call
只是一个相关的提示 - 因为正如其他答案所说,分组是一个 IEnumerable,如果您需要访问特定索引,您可以使用
group.ElementAt(i)
。这对很多人来说可能是显而易见的,但希望它能对一些人有所帮助!
Just a related tip - since, as the other answers have said, the grouping is an IEnumerable, if you need to access a specific index you can use
group.ElementAt(i)
.This is probably obvious to a lot of people but hopefully it will help a few!