【问题标题】:How to get the summary of items in a List using Linq如何使用 Linq 获取列表中项目的摘要
【发布时间】:2013-09-27 07:50:41
【问题描述】:
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. Shelf 1 Row 1 是第一个货架第一行的名称。

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

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

【问题讨论】:

    标签: c# .net vb.net linq


    【解决方案1】:

    我认为你必须改变你的数据结构

    我会这样做

    货架列表 --> 行列表 --> 产品列表

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

    每个架子

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

    【讨论】:

    • 但是按照设计,这是不可能的。
    【解决方案2】:

    你可以嵌套 LINQ 查询,例如(对不起,我是一个 C-sharp man);

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多