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. What would some cleaner code be that I can use to get the Dictionary<string, string> as my result?
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());
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…