在C#中使用LINQ进行字典操作

Tho*_*eld 2 c# linq lambda dictionary

我有一个字典

Dictionary<String, List<String>> MyDict = new Dictionary<string, List<string>>
{
    {"One",new List<String>{"A","B","C"}},
    {"Two",new List<String>{"A","C","D"}}
};
Run Code Online (Sandbox Code Playgroud)

我需要List<String>从这本字典中获取一个,列表应包含上述字典值的不同项.

因此生成的List将包含{"A","B","C","D"}.

现在我正在使用for循环和Union操作.喜欢

List<String> MyList = new List<string>();
for (int i = 0; i < MyDict.Count; i++)
{
    MyList = MyList.Union(MyDict[MyDict.Keys.ToList()[i]]).Distinct().ToList();
}
Run Code Online (Sandbox Code Playgroud)

任何人都可以建议我在LINQ或LAMBDA Expression中执行此操作.

Mar*_*ell 7

var items=MyDict.Values.SelectMany(x=>x).Distinct().ToList();
Run Code Online (Sandbox Code Playgroud)

或者替代方案:

var items = (from pair in MyDict
             from s in pair.Value
             select s).Distinct().ToList();
Run Code Online (Sandbox Code Playgroud)