本文介绍了比较值是否在范围内的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一堂课
public class VitalSign
{
public double? Temprature { get; set; }
public double? SystolicBP { get; set; }
public double? DiastolicBP { get; set; }
public double? PulseRate { get; set; }
public double? Respirations { get; set; }
}
和一个范围类
public class VitalRange
{
public Tuple<double,double> TemperatureRange { get; set; }
public Tuple<double, double> SystolicBPRange { get; set; }
public Tuple<double, double> DiastolicBPRange { get; set; }
public Tuple<double, double> RespirationsRange { get; set; }
public Tuple<double, double> PulseRange { get; set; }
}
我必须比较我的属性(比如温度)与 VitalRange(温度)相比是否超出范围.在这种情况下,我可以使用元组,还是可以为范围设置 KVP?.我将把一个巨大的列表与 VitalRange 进行比较.检查属性是否在范围内的最快方法是什么?
I have to compare if my property(say temperature) is out of range compared to VitalRange( Temperature). Can I use a Tuple in this case or can I have a KVP for ranges?. I will be comparing a huge List against VitalRange. What will be the fastest way to check if a property falls within the range.?
推荐答案
不知道你的问题是什么,很简单if
,但是你可以用扩展方法封装:
I don't know what's your problem, It's simple if
, but you can encapsulate it by using extension methods:
public static bool IsInRange(this double? input, Tuple<double, double> range)
{
if (!input.HasValue)
return false;
return input >= range.Item1 && input <= range.Item2;
}
并像这样使用它:
VitalRange sampleRanges = ....;
var validTempers = vitalSignList.Where(x=>x.Temprature.IsInRange(sampleRange.VitalRange);
我想不出极快的方法,但我更喜欢封装.
I can't think for extremely faster way, but I prefer encapsulation.
这篇关于比较值是否在范围内的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!