特定的plinq查询

时间:2011-07-14 12:03:13

标签: c# dictionary plinq

我想在C#dictionary<int, double>

中选择一个具有最大值的ID

例如

初始词典

 1   0.8   
 2   0.78   
 3   0.9   
 4   0.87   
选择

 3   0.9   

我当前的代码

dic.AsParallel().Max(x => x.Value);

3 个答案:

答案 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()?

怎么样?