使用linq的字典中的最小值 [英] minimum value in dictionary using linq

查看:125
本文介绍了使用linq的字典中的最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类型为

的字典

 字典< DateTime,double>字典

如何使用linq从该字典中检索与该值相对应的最小值和键? / p>

解决方案

聚合



  var minPair = dictionary.Aggregate((p1,p2)=>(p1.Value< p2.Value)?p1:p2); 

使用强大的 Aggregate p>

我知道在这种情况下, MinBy 更清洁,但是使用 Aggregate 你有更多的权力和它的内置。 ;)


I have a dictionary of type

Dictionary<DateTime,double> dictionary

How can I retrive a minimum value and key coresponding to this value from this dictionary using linq ?

解决方案

Aggregate

var minPair = dictionary.Aggregate((p1, p2) => (p1.Value < p2.Value) ? p1 : p2);

Using the mighty Aggregate method.

I know that MinBy is cleaner in this case, but with Aggregate you have more power and its built-in. ;)

这篇关于使用linq的字典中的最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆