Arrays VB.NET-根据值将对象数组拆分为更小的数组
我有一个类型为Arrays VB.NET-根据值将对象数组拆分为更小的数组,arrays,vb.net,multidimensional-array,split,Arrays,Vb.net,Multidimensional Array,Split,我有一个类型为Material的数组,我想将其分成更小的组。材料是由类型、说明和值组成的对象。我想显示按类型分组的材料的说明和值,这样我可以像这样显示它们: For Each matTypeGroup As Material() In matTypeGroups DisplayTypeName(matTypeGroup(0).Type) For Each mat As Material In matTypeGroup DisplayMaterialInfo(m
Material
的数组,我想将其分成更小的组。材料
是由类型
、说明
和值
组成的对象。我想显示按类型
分组的材料的说明
和值
,这样我可以像这样显示它们:
For Each matTypeGroup As Material() In matTypeGroups
DisplayTypeName(matTypeGroup(0).Type)
For Each mat As Material In matTypeGroup
DisplayMaterialInfo(mat.Description, mat.Value)
Next
Next
- Type1
Description1: Value1
Description2: Value2
Description3: Value3
- Type2
Description4: Value4
- Type3
Description5: Value5
Description6: Value6
最终输出如下所示:
For Each matTypeGroup As Material() In matTypeGroups
DisplayTypeName(matTypeGroup(0).Type)
For Each mat As Material In matTypeGroup
DisplayMaterialInfo(mat.Description, mat.Value)
Next
Next
- Type1
Description1: Value1
Description2: Value2
Description3: Value3
- Type2
Description4: Value4
- Type3
Description5: Value5
Description6: Value6
如何将材料
数组拆分为按类型分组的材料
数组?
您可以使用:
您只需编辑代码即可使用iGroup而不是数组。我假设这里的类型是字符串
For Each matTypeGroup As IGrouping(Of String, Material) In matTypeGroups
DisplayTypeName(matTypeGroup.Key)
For Each mat As Material In matTypeGroup
DisplayMaterialInfo(mat.Description, mat.Value)
Next
Next
这也是我的答案。虽然此答案不会返回数组,但会按您想要的方式对其进行分组。如果您需要知道如何使用它,请在MSDN上查找IGrouping。难道您不能调用.ToList()并像处理数组一样使用它-使用列表中的项目(按索引)。您可以,但在这种情况下,直接使用IGrouping也可以。