LINQ查询返回Dictionary <string,string> - LINQ query to return a Dictionary<string, string>

问题:

I have a collection of MyClass that I'd like to query using LINQ to get distinct values, and get back a Dictionary<string, string> as the result, but I can't figure out how I can do it any simpler than I'm doing below. 我有一个MyClass的集合,我想用LINQ来查询获取不同的值,然后得到一个Dictionary <string,string>作为结果,但我无法弄清楚我怎么能比我更简单我在下面做。 What would some cleaner code be that I can use to get the Dictionary<string, string> as my result? 一些更干净的代码可以用来获取Dictionary <string,string>作为我的结果?

var desiredResults = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

var queryResults = (from MyClass mc in myClassCollection
                    orderby bp.SomePropToSortOn
                    select new KeyValuePair<string, string>(mc.KeyProp, mc.ValueProp)).Distinct();

foreach (var item in queryResults)
{
    desiredResults.Add(item.Key.ToString(), item.Value.ToString());
}

解决方案:

参考一: https://en.stackoom.com/question/2dKt
参考二: https://stackoom.com/question/2dKt
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章