2017-02-22 81 views
1

我有一個List<KeyValuePair<string, List<string>>,所以數據看起來類似:合併列表

user1, [trigger1,trigger2,trigger3] 
user2, [trigger1,trigger4] 
user3, [trigger2,trigger3,trigger4] 
user1, [trigger0,trigger4] 

我想使這個Dictionary<string,List<string>>,而是直接失敗,因爲會有重複的鍵。有沒有辦法在LINQ上動態合併附加值的內容(意味着如果用戶1是鍵的兩倍,將兩個關聯的Lists<>合併在一起)?

回答

3

您需要在Key上對KVP進行分組,並使用SelectMany將列表「拼合」。如果你不想重複,使用Distinct()將其刪除:

var res = list 
    .GroupBy(p => p.Key) 
    .ToDictionary(
     g => g.Key 
    , g => g.SelectMany(p => p.Value).Distinct().ToList() 
    ); //        ^^^^^^^^^^ 
     // Remove Distinct() if you would like to keep all items 
+0

看起來不錯,但我不需要list1.Concat(列表2),因爲我只有1名名單。 – FailedUnitTest

+0

@FailedUnitTest我認爲你的KVP來自不同的來源。謝謝! – dasblinkenlight

1

一個GroupBy會做的大部分工作,以使所有的分組項目的列表結合的和你做。 GroupBy的結果包含一個枚舉列表,所以我們必須使用SelectMany來選擇所有字符串項目並從中構建一個列表。

Dictionary<string, List<string>> d = l.GroupBy(k => k.Key) 
             .ToDictionary(k => k.Key 
                , k => k.SelectMany(s => s.Value).ToList() 
                ); 
1

您可以通過以下順序的一系列鏈式LINQ查詢做到這一點:

  • GroupBy() - 這將創建每個單獨的用戶羣,以避免出現問題創建字典時(即重複密鑰)。
  • ToDictionary() - 此方法將使用鍵和值綁定給定的一組數據,因爲您已經有了鍵(通過先前的調用),您只需從每個組中獲取值。
  • SelectMany() - 這將選擇您之前的組中的所有單個值,實質上是將每組用戶中的所有項目合併到一個集合中。
  • Distinct() - 這將應用在上一步中生成的字符串列表中,以刪除任何重複項。如果你想允許重複,只需刪除這一步。

實現這個看起來像下面的代碼片段:

// Group the collections by user and then select the keys based on those users 
var dictionary = list.GroupBy(l => l.Key) 
        .ToDictionary(
          x => x.Key, 
          x => x.SelectMany(l => l.Value) 
            .Distinct() 
            .ToList() 
        ); 

// Example of your existing data 
var list = new List<KeyValuePair<string, List<string>>>(){ 
     new KeyValuePair<string,List<string>>("User 1", new List<string>(){ "trigger1" ,"trigger2", "trigger3" }), 
     new KeyValuePair<string,List<string>>("User 2", new List<string>(){ "trigger1" ,"trigger2" }), 
     new KeyValuePair<string,List<string>>("User 3", new List<string>(){ "trigger2" ,"trigger3", "trigger4" }), 
     new KeyValuePair<string,List<string>>("User 1", new List<string>(){ "trigger0" ,"trigger4" }), 
}; 

// Group the collections by user and then select the keys based on those users 
var dictionary = list.GroupBy(l => l.Key) 
        .ToDictionary(
          x => x.Key, 
          x => x.SelectMany(l => l.Value) 
            .Distinct() 
            .ToList() 
        ); 

// Output each key with it's associated values 
foreach(var key in dictionary.Keys) 
{ 
     Console.WriteLine("Key: " + key + ", Values: " + String.Join(",",dictionary[key].ToArray())); 
} 

// Example output 
// Key: User 1, Values: trigger1,trigger2,trigger3,trigger0,trigger4 
// Key: User 2, Values: trigger1,trigger2 
// Key: User 3, Values: trigger2,trigger3,trigger4 

你可以see an interactive version of this here

0

試試這個:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Data; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 

     static void Main(string[] args) 
     { 
      List<KeyValuePair<string, List<string>>> myList = new List<KeyValuePair<string,List<string>>>() { 
       new KeyValuePair<string, List<string>>("user1", new List<string> { "trigger1","trigger2","trigger3"}), 
       new KeyValuePair<string, List<string>>("user2", new List<string> { "trigger1","trigger4"}), 
       new KeyValuePair<string, List<string>>("user3", new List<string> { "trigger2","trigger3","trigger4"}), 
       new KeyValuePair<string, List<string>>("user1", new List<string> { "trigger0","trigger4"}) 
      }; 

      Dictionary<string, List<string>> dict = myList 
       .GroupBy(x => x.Key, y => y) 
       .ToDictionary(x => x.Key, y => y.Select(z => z.Value).SelectMany(a => a).ToList()); 

     } 
    } 
}