我可以检查 Double 值是否在 Dictionary<double,double> 内的对值之间

时间:2021-03-01 19:58:14

标签: c#

我有以下内容:-

double v = 1.1;
Dictionary<double,double> cleanedTimes = new Dictionary<double,double>();
//code for building the cleanedTimes goes here..

那么我如何检查 v 是否在 cleanedTimes 项内的任何对值之间?

谢谢

1 个答案:

答案 0 :(得分:0)

您可以将 Any() 与您喜欢的任何条件一起使用。您的条件就像“大于键,但小于值或相反”一样简单。所以表达式/代码可以是这样的:

IDictionary<double, double> values = new Dictionary<double, double>();
values.Add(1.1, 2.2);
values.Add(3.3, 4.4);
values.Add(5.5, 5.0);

double check = 1.5;

bool found = values.Any(it => (it.Key <= check && check <= it.Value) ||
                              (it.Value <= check && check <= it.Key));
Console.WriteLine(found);

这将打印 True,因为 check 的值 1.5 介于 1.12.2 之间。