繁体   English   中英

如何使用Linq获取列表中项目的摘要

[英]How to get the summary of items in a List using Linq

Public Class Product
public  Name As String
public  ID as String
public  ShelfList as List(of Shelf)
End Class 

Public Class Shelf
public  Name As String
public  ID as String
public  Row as List(of Row)
End Class

Public Class Row
public  Name As String
public  ID as String
End Class

以上是我的课程。 假设可以在不同的货架和不同的行中找到产品。 一排也可以容纳多个产品。

数据将与此类似。

List(of Products) -> List(of Shelf) -> List(of Rows)

以下是类似数据

Boost ->  Shelf 1 -> Row 1,Row 2 
          Shelf 2 -> Row 2

Horlicks ->Shelf 1-> Row 1

Complain ->Shelf 2-> Row 2,Row 3

Colgate -> Shelf 2- Row 3

因此,从上面的产品列表中,我需要生成摘要。 (每行的行名和项目数)

   Shelf 1 Row 1 - 2 (Boost and horlicks)
   Shelf 1 Row 2 - 1 (Boost)
   Shelf 2 Row 1 - 1 (Horlicks)
   Shelf 2 Row 2 - 2 (Complain and Boost)
   Shelf 2 Row 3 - 1 (Colgate)

注意 :

  1. 架子1第1行是第一个架子的第一行的名称。

  2. 无需在括号中显示项目名称。 现在只是为了理解。

如何使用LINQ实现这一目标。 我正在使用VB.NET,并以CE 3.5为目标。 C#建议也表示赞赏。 提前致谢。

我认为您必须更改数据结构

我会这样

货架清单->行清单->产品清单

为什么产品会跟踪该行? 这是包含不同产品的行。

对于每个架子

选择货架号->行号->产品名称

例如,您可以嵌套LINQ查询(对不起,我是个C尖锐的人)。

var a = from t1 in Products
        select new 
        {
             Name = t1.Name,
             Summary = String.Join(", ",(from t2 in t1.ShelfList
                        select t2.Name))
        };

这样的事情应该做,尽管我忽略了第三层。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM