开发者

a specific plinq query

i want to select one id that have max value in a C# dictionary<int, double>

for example

initial dictionary

 1   0.8   
 2   0.78   
 3   0.9   
 4   0.87   

after select

 3   0.9   

my开发者_开发知识库 current code

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


It sounds like you want a MaxBy construct. That doesn't exist in normal LINQ to Objects, although I have one in MoreLINQ, so you'd write:

var result = dictionary.MaxBy(pair => pair.Value);

However, that won't be parallelized in the way that you'd want. You could at least try using this overload of ParallelEnumerable.Max having created your own comparable type containing the relevant values:

public class ComparablePair : IComparable<ComparablePair>
{
    // Have key and value, then implement IComparable<T> by
    // comparing values
}

Then use:

var result = dictionary.AsParallel()
                       .Max(pair => new ComparablePair(pair));


dic.AsParallel().First(y => y.Value == dic.Select(x => x.Value).Select(x => x.Max())).Key


What about OrderBy(x => x.Value).First() ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜