Dictionary 查找相近的两个值。
我用Dictionary存了一张表,key 为温度t(0—200),value为值,现在我有一个值,需要在这张表中找与之最相近的两个value,然后取出value和对应的t。怎么实现相近值的查找啊? --------------------编程问答-------------------- from entry in myDict orderby entry.Value ascending select entryhttp://stackoverflow.com/questions/289/how-do-you-sort-a-dictionary-by-value --------------------编程问答--------------------
打不开啊。 --------------------编程问答-------------------- 200个的级别倒是可以逐个比较:
--------------------编程问答--------------------
Dictionary<string, int> dict = new Dictionary<string, int>()
{
{"one", 1},
{"two", 2},
{"nine", 9},
{"five", 5},
};
int toCompare = 8;
var pair = dict.Aggregate(dict.First(), (next, result) => Math.Abs(next.Value - toCompare) < Math.Abs(result.Value - toCompare) ? next : result);
string key = pair.Key; // "nine";