我有一个SQL数据库,其中包含不同的值:名称、尺寸、重量、价格和材料。
每个产品可以有多种材料。如下例所示:产品1具有3种材料,产品2具有2种材料。
名称 | 尺寸 | |||||
---|---|---|---|---|---|---|
产品1 | 340 | 3.0 | 3.55>钢 | |||
产品1 | 340 | 3.0 | 3.55>黄铜||||
产品1 | 340 | 3.0 | 产品2 | 214 | <2.3>3.25钢铁 | [/tr>|
产品2 | 214 | <2.3>3.25黄金 |
所以实际上您正在使用ADO.NET将整个表加载到内存中(DataTable
(。如果它不会增长,那也没关系,但它不是一种可扩展的方法。然而
例如,您可以在包含产品字段的ValueTuple上使用GroupBy
:
Dim products As List(Of Product) = table.AsEnumerable().
GroupBy(Function(row) (Name:=row.Field(Of String)("Name"), Dimension:=row.Field(Of Int32)("Dimension"), Weight:=row.Field(Of Decimal)("Weight"), Price:=row.Field(Of Decimal)("Price"))).
Select(Function(g) New Product With
{
.name = g.Key.Name,
.dimension = g.Key.Dimension,
.weight = g.Key.Weight,
.price = g.Key.Price,
.materials = g.Select(Function(row) row.Field(Of String)("Material")).ToArray()
}).
ToList()