【问题标题】:VB.New How to Iterate LINQ Group with multiple groupingsVB.New 如何使用多个分组迭代 LINQ 组
【发布时间】:2014-12-30 01:04:46
【问题描述】:

我花了几个小时浏览这里和网络上的档案,以了解如何迭代具有多个 VB.Net 分组的 IGroup。 C# 有很多解决方案,但我真的很难转换到 VB.Net。

这是我要分组的列表

dim merge_list As List(Of MergeData)

这是我按列表中的三个属性分组的代码

 Dim groups = merge_list.GroupBy(Function(t) New With {Key t.GUID.Category,
                                       Key t.GUID.FeatureType,
                                       Key t.GUID.AssetType}) _
                                       .Where(Function(grp) grp.Count > 1)

接下来我尝试遍历组,但由于我没有组列表中项目的类型(即暗淡组 As SomeType),我不知道如何使用它们。

这是我循环遍历组的代码

dim group '===note there is no "As SomeType" for group just 'Dim group'===
For Each group In groups
  ' since group is not typed I cannot figure out how to work with it 

next

谁能告诉我如何使用 VB.NET 迭代具有多个组的 IGoup?

【问题讨论】:

  • 你不需要在For Each之前声明group。这种方式编译器应该找出groups中的项目类型是什么。
  • 这个问题被否决了。我该如何改进它?
  • 是的,我确实需要在使用 VB 时声明组,如果不这样做,我会收到编译器错误:“'group' 未声明。由于其保护级别,它可能无法访问” .我更喜欢不需要这样做的 C#。 VB 令人沮丧。
  • 嗯,我刚试过,你没有。检查我的答案。
  • @scott_f - 在 VB 中也不需要指定类型。检查我的答案并附上 Fiddle。

标签: vb.net linq igrouping


【解决方案1】:

您不必在For Each 语句之外声明group。查看以下代码:

Dim source = Enumerable.Range(0, 1000)

Dim groups = source.GroupBy(Function(x) New With {
                                Key .ByFive = x Mod 5,
                                Key .ByTen = x Mod 10,
                                Key .ByTwenty = x Mod 20})

For Each group In groups
    Console.WriteLine("ByFive: {0}, ByTen: {1}, ByTwenty: {2}, Count: {3}",
                  group.Key.ByFive, group.Key.ByTen, group.Key.ByTen, group.Count())
Next

它编译得很好并打印:

ByFive: 0, ByTen: 0, ByTwenty: 0, Count: 50
ByFive: 1, ByTen: 1, ByTwenty: 1, Count: 50
ByFive: 2, ByTen: 2, ByTwenty: 2, Count: 50
ByFive: 3, ByTen: 3, ByTwenty: 3, Count: 50
ByFive: 4, ByTen: 4, ByTwenty: 4, Count: 50
ByFive: 0, ByTen: 5, ByTwenty: 5, Count: 50
ByFive: 1, ByTen: 6, ByTwenty: 6, Count: 50
ByFive: 2, ByTen: 7, ByTwenty: 7, Count: 50
ByFive: 3, ByTen: 8, ByTwenty: 8, Count: 50
ByFive: 4, ByTen: 9, ByTwenty: 9, Count: 50
ByFive: 0, ByTen: 0, ByTwenty: 10, Count: 50
ByFive: 1, ByTen: 1, ByTwenty: 11, Count: 50
ByFive: 2, ByTen: 2, ByTwenty: 12, Count: 50
ByFive: 3, ByTen: 3, ByTwenty: 13, Count: 50
ByFive: 4, ByTen: 4, ByTwenty: 14, Count: 50
ByFive: 0, ByTen: 5, ByTwenty: 15, Count: 50
ByFive: 1, ByTen: 6, ByTwenty: 16, Count: 50
ByFive: 2, ByTen: 7, ByTwenty: 17, Count: 50
ByFive: 3, ByTen: 8, ByTwenty: 18, Count: 50
ByFive: 4, ByTen: 9, ByTwenty: 19, Count: 50

For Each 中处理group 时,您甚至会获得智能感知!

【讨论】:

  • 是什么让你兴奋不已?使用您的代码时,我得到相同的编译器错误。我截图证明我没有疯:drive.google.com/a/slingblade.co.nz/file/d/…。它必须是 Visual Studio 中的设置吗?我会搜索看看我能找到什么。
  • 我不得不手动将 Option Strict Off 和 Option Infer On 添加到我的课程中,即使这两个标志都在 Tools>Options>Project and Settings>VB Defaults 设置区域中设置。一旦我这样做了,我就可以使用智能感知了。感谢您的帮助!
  • 有人对我的问题投了反对票(他发布了一个可怕的答案 [C# 和我的 VB.Net 代码的混合,没有文字,只有代码],我礼貌地指出了它的问题,然后他删除了回答并否决了我的问题)。如果您认为这是一个可以的问题,您可以投票吗?我试图赚取声望点,这很伤人。
  • 您永远不应该要求人们支持您的问题。您可以通过提出好问题、编辑其他问题等来获得声誉积分。请耐心等待。要求投票不是正确的方式。
【解决方案2】:

像这样简单地使用嵌套的 For Each:-

 For Each item In groups
        Console.WriteLine(item.Key.GUID.Category) //You can access only `Keys` here
        For Each inneritem In item
              Console.WriteLine(inneritem.GUID.Category)
               //so on (You can access all the properties here)
        Next
 Next

检查这个Fiddle

【讨论】:

  • 谢谢! Fiddle 似乎目前处于关闭状态。
  • 哎呀哦。不过它对我来说是开放的。
  • 是的,它现在适合我,奇怪。不错的网站,第一次听说。我加入了。
猜你喜欢
  • 1970-01-01
  • 2011-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多