2013-09-27 90 views
0
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 

以上是我的課程。 假設產品可以在不同的貨架和不同的行中找到。 另外一排可以容納多種產品。如何使用Linq獲取列表中項目的摘要

數據將與此類似。

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#建議也表示讚賞。提前致謝。

回答

0

我認爲你必須改變你的數據結構

我會做這樣的

保質列表 - >行的名單 - 產品

爲什麼會>產品一覽跟蹤行? 它是包含不同產品的行。

對於每個貨架

選擇現有的號碼 - >行號 - >產品名稱

+0

但按照設計,這是不可能的。 – kbvishnu

0

您可以嵌套LINQ查詢,例如(對不起,我是一個C-犀利哥);

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

這樣的事情應該做,雖然我忽略了第三層。

相關問題