2014-10-27 56 views
2

除了我的previous question,我仍然沒有具體的解決方案來解決我的問題。將重複選項合併成一個結果

我有2類這樣的:

public class Product 
{ 
    public Product() 
    { 
     Options = new List<Option>(); 
    } 
    public string Name { get; set; } 
    public int ID { get; set; } 
    public List<Option> Options { get; set; } 
} 

public class Option 
{ 
    public int SubID { get; set; } 
    public string SubName { get; set; } 
    public int ListBoxID { get; set; } 
    public decimal Price { get; set; } 
    public bool IsSelected { get; set; } 
} 

哪些是在這樣的列表中使用:

List<Product> Products { get; set; } 

enter image description here

當用戶在左側選擇產品(其總是具有相同的屬性),它的內部List<Optie>將獲得4個不同的加載ListBox's,過濾ListBoxID

現在,如果用戶在2種產品中選擇了相同的選項,那麼如何使用linq將它們轉換爲一個結果呢?

例如4級相同的產品被加載,用戶選擇2個產品相同的選項,那麼我的結果將是:

2x Product 2x lookbrood 2x ollema 2x fitness broodje 2x testtest

如果2種其他產品有不同的選擇,這可能是他們的結果:

2x Product 2x ciabatta broodje 2x ollema 2x slagroom 2x smos

我總是需要的量裝入Products: 如:6個產品被加載,那麼我的組合可以是:

3x Product 3x ciabatta broodje 3x ollema 3x slagroom 3x smos

3x Product 3x Some other option 1 3x Some other option 2 3x Some other option 3 3x Some other option 4

2x Product 2x Some option 1 2x Some option 2 2x Some option 3 2x Some option 4

2x Product 2x Some other option 1 2x Some other option 2 2x Some other option 3 2x Some other option 4

2x Product 2x Some other option 1 2x Some other option 2 2x Some other option 3 2x Some other option 4

這是我能給的最好的解釋,我希望像幫助。

+0

這可能是更清楚,如果你可以給他們的'Optie'(選項?)什麼結果應該如果這些2' Product'選擇喜歡的2'Product'的例子, 。最終結果的類型應該是什麼?它應該是一個單一的「產品」?一個'清單'? Optie應該如何組合? – 2014-10-27 14:06:05

+1

Argh ...你爲什麼要混合這樣的語言?你有一個名爲IsSelected(nice,English name)的屬性...然後是一堆非英語屬性(包括類名)。如果你從別的國家得到一個編碼器,你的代碼將與他/她合作。除此之外,我無法弄清楚你想做什麼。 :| – Shaamaan 2014-10-27 14:06:11

+0

@MattBurland更新了我的問題,這是一件複雜的事情,我知道.. – DeMama 2014-10-27 14:20:23

回答

2

您是否在尋找GroupBy?。這會將Optie s分組爲相同的ID,並給出它們的數量。

Product.Opties 
    .GroupBy(x => new { 
     x.ListBoxID, x.Naam 
    }) 
    .Select(x => 
    new { 
     ListBoxID = x.Key.ListBoxID, 
     Naam = x.Key.Naam, 
     Count = x.Count() 
    }) 

More info on GroupBy