简体   繁体   English

C#代码/表达式以查找某个值在-的范围内

[英]C# Code/Expression to find which range a certain value falls within --

I will just explain in brief what I have. 我将简单地解释一下我所拥有的。

I have a list of configuration data like this -- 我有这样的配置数据列表- 在此处输入图片说明

Now I am trying to write code to find out which set/config a certain user falls, based upon his purchase sales amount. 现在,我正在尝试编写代码,以根据某位用户的购买销售额来确定该用户属于哪个设置/配置。 for eg. 例如 if he has made a sale of 23000 bucks he becomes a G01 grade salesguy. 如果他的销售额达到23000美元,那么他将成为G01级销售人员。 If he made 51000 bucks, he is G04 grade. 如果他赚了51000美元,他就是G04级。 If he made sale of 29000 bucks he would be G02 grade. 如果他卖29000美元,他将是G02等级。 Or if he made 71000 he is obviously G05 grade. 或者,如果他的成绩是71000,那么他显然是G05级。 Like that. 像那样。 To explain my use-case. 解释我的用例。

The code/logic I wrote was something like - 我写的代码/逻辑是这样的-

public JsonResult CustomerGradeByID([FromBody]decimal saleAmt)
{
    try
    {
        var cGrade = CustomerGrade(Convert.ToDecimal(saleAmt));
        return Json(cGrade);
    }
    catch (Exception exp)
    {
        return Json(exp.GetBaseException());
    }
}
protected string CustomerGrade(decimal salesTot)
{
    try
    {
        var grades = _appDbContext.CustomerGrades.ToList();
        CustomerGrade cg = grades.Aggregate((a, b) => a.grade_minsaleamount < salesTot && salesTot < b.grade_minsaleamount ? a : b);
        var gdName = cg.grade_name.ToString();
        return gdName;
    }
    catch(Exception exp)
    {
        throw exp;
    }
}

Don't worry about _appdbcontext and all that I am just fetching and populating from my db where that table is stored. 不用担心_appdbcontext以及我只是从存储该表的数据库中获取和填充的所有内容。 Apparently the logic for 'CustomerGrade' is wrong. 显然,“ CustomerGrade”的逻辑是错误的。 Because it is giving me all kinds of haywire results. 因为它给了我各种各样的结果。 For 33000 bucks sale it gives G01, for 61000 bucks it gives G04 and similar other all kinds of erroneous outputs. 对于33000美元的销售,它给出G01,对于61000美元的它给出G04和类似的所有其他错误输出。 I tried using lambda aggregate. 我尝试使用lambda聚合。 But seems it is incorrect. 但是似乎不正确。

Can you help me perfect this logic please? 你能帮我完善这个逻辑吗? A bulletproof logic for this scenario? 这种情况下的防弹逻辑? It would be hugely helpful for me! 这对我有很大帮助! Thanks to all, 谢谢大家,

This is my actual set of data -- 这是我的实际数据集-

在此处输入图片说明

Assuming grade_minsaleamount are unique, you can just order the list and use FirstOrDefault i guess, it can all be done on the DB 假设grade_minsaleamount是唯一的,我想您可以只订购列表并使用FirstOrDefault ,所有这些都可以在数据库上完成

var result = _appDbContext.CustomerGrades
                          .OrderBy(x => x.grade_minsaleamount)
                          .FirstOrDefault(x => salesTot >= x.grade_minsaleamount);

// Sanity check for null
if(result != null)
{
   // found!
   Debug.WriteLine(result.Name);
}

Additional Resources 其他资源

Enumerable.FirstOrDefault Method Enumerable.FirstOrDefault方法

Returns the first element of a sequence, or a default value if no element is found. 返回序列的第一个元素,如果找不到元素,则返回默认值。

Enumerable.OrderBy Method Enumerable.OrderBy方法

Sorts the elements of a sequence in ascending order. 按升序对序列的元素进行排序。

Update 更新资料

_appDbContext.OrderBy(x => x.MinSaleAmount)
                      .LastOrDefault(x => x.MinSaleAmount < salesTot);

Ok what this is saying is 好的,这是在说什么

Order the sales amount like you have in your table. 像表中一样订购销售额。 Filter out anything above that sales amount, and choose the last one. 过滤掉超出该销售额的任何东西,然后选择最后一个。

If salesTot is higher then the last items in the list, it will return the last item in the list. 如果salesTot高于列表中的最后一项,它将返回列表中的最后一项。 if it is less than the minimum it will return null. 如果小于最小值,则将返回null。

The confusion here was basically the concept if minimum sales amount, to be a G02 they cannot make less than 25000, or they are G01. 这里的困惑基本上是一个概念,即最小销售量是不能赚到25000的G02,或者是G01。 However there is no upper limit. 但是没有上限。 Though there is a finite lower limit 虽然下限是有限的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM