如何从数据库中获取最接近的上限值

时间:2015-09-22 06:36:13

标签: asp.net-mvc database

我有一个数据库,其中包含不同的重量费用值,如果总重量是2.5公斤,那么我想设置一个条件,从数据库中选择最接近的上限值,如果是avaivalbe,则需要3公斤的费用。

我有这个代码,在总重量的基础上我想选择最接近的上限值费用和总重量成本:

public ActionResult LoadweightByServices(int service_id, float totalweight) { 
    OrderModel om = new OrderModel();                  
    Subcategory_ _Subcategory = new Subcategory_(); 
    var we = om.Get_Shipping_weight(service_id).ToList(); 
    double? weight = we.SingleOrDefault().Weight; 
    if (totalweight == weight || totalweight < weight) 
    { 
    float? cost=we.SingleOrDefault().Cost; 
    float? charge=we.SingleOrDefault().Charge; 
    } 

    return View()
     }

2 个答案:

答案 0 :(得分:0)

public ActionResult LoadweightByServices(int service_id, float totalweight) { 
OrderModel om = new OrderModel();                  
Subcategory_ _Subcategory = new Subcategory_(); 
var we = om.Get_Shipping_weight(service_id).ToList(); 
double? weight = we.SingleOrDefault().Weight; 
if (Math.Ceiling(Convert.ToDouble(totalweight)) == weight || Math.Ceiling(Convert.ToDouble(totalweight)) < weight) 
{ 
float? cost=we.SingleOrDefault().Cost; 
float? charge=we.SingleOrDefault().Charge; 
} 

return View()
 }

答案 1 :(得分:0)

使用CEILING功能。

例如Select Ceiling(1.1)返回2

More info

  

返回大于或等于指定数值表达式的最小整数。

在您的代码中,尝试

weight = Math.Ceiling(Convert.ToDecimel(we.SingleOrDefault().Weight));