Linq-获得介于下限和上限之间的值 [英] Linq - getting a value that is between a lower limit and upper limit
问题描述
我有一个费用清单(linq到sql实体-如果相关)-费用上限,费用下限(均为十进制值).我传入了property value
-例如90000,我想检查此property
值最佳是否匹配 one (或 first 值)费用列表中的一个.
I have a list of Fees (linq to sql entity- if relevant) - Upper Fee, Lower Fee (both are decimal values). I am passing in a property value
- say 90000 and I want to check if this property
value best matches one (or first value out of many) from the list of fees.
费用可能是...
(较低的费用-较高的费用)
0 - 50000
50001 - 75000
75001 - 90000
90001 - 140000
190000 - 500000
从这些值中,最适合90000匹配 75001-90000频段,因此我想提取该 FeeDetail 实体.我真的不知道该使用什么运算符,不胜感激,到目前为止我的代码是...
Out of these values, 90000 is best matched for something like 75001 - 90000 band, so I want to pull out that FeeDetail entity. I dont really know what operator to use, any help is appreciated, my code so far is...
[Test]
public void GetFeeRange()
{
const int id = 44;
var propValue = 90000;
//get a specific fee entity, then get the range of fee details...
var recommendedFees = RepoSession.All<Fee>()
.Where(x =>
x.ClientSurveyTypeID == id)
.GroupJoin(_readOnlySession.All<FeeDetail>(),
x => x.FeeID,
y => y.FeeID,
(x, y) => new
{
FeeDetail = y.DefaultIfEmpty()
})
.Select(x => x.FeeDetail)
.SingleOrDefault();
Assert.IsNotNull(recommendedFees);
//order fees by lowest fee - *the bit I am stuck on*
var bestMatch = recommendedFees.OrderBy(x => x.Lower)
.TakeWhile(x => propValue >= x.Lower || propValue <= x.Upper)
.FirstOrDefault();
}
问题-我将如何执行范围检查?我需要什么linq运算符?不确定我是否应该花点时间从那范围内获得最好的费用?
Question- how would I perform the range checks? What operator of linq do I need? Not sure if I should perform a takewhile then, get the best fee from that range?
注意:收费很容易...
NOTE: the fees could very easily be...
(较低的费用-较高的费用)
0 - 50000
50001 - 75000
95001 - 140000
190000 - 500000
如果找到最佳匹配,将其拔出 OR 以获得最接近的匹配...如果一个匹配(不够)完全匹配,也许我可以显示可用匹配的列表>
If a best match is found, pull that out OR get the closest match... maybe I can show a list of available matches if one is not (near enough) an exact match
推荐答案
简单的.Where
应该可以:
var bestMatch = recommendedFees
.Where(x => propValue >= x.Lower && propValue <= x.Upper)
.OrderBy(x => x.Lower)
.FirstOrDefault();
这篇关于Linq-获得介于下限和上限之间的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!