我想在C#dictionary<int, double>
例如
初始词典
1 0.8
2 0.78
3 0.9
4 0.87
选择后
3 0.9
我当前的代码
dic.AsParallel().Max(x => x.Value);
答案 0 :(得分:3)
听起来你想要一个MaxBy
构造。普通的LINQ to Objects中不存在这种情况,虽然我在MoreLINQ中有一个,所以你要写:
var result = dictionary.MaxBy(pair => pair.Value);
但是,这不会以您想要的方式并行化。您至少尝试使用this overload of ParallelEnumerable.Max
创建了您自己的可比类型,其中包含相关值:
public class ComparablePair : IComparable<ComparablePair>
{
// Have key and value, then implement IComparable<T> by
// comparing values
}
然后使用:
var result = dictionary.AsParallel()
.Max(pair => new ComparablePair(pair));
答案 1 :(得分:1)
dic.AsParallel().First(y => y.Value == dic.Select(x => x.Value).Select(x => x.Max())).Key
答案 2 :(得分:0)
OrderBy(x =&gt; x.Value).First()?
怎么样?