0

如何更改查询中的值,例如:

        return (from cust in entities.vw_WebCustomer
                where cust.CorporationId == token.CorporationId &&
                      cust.Branch == branch &&
                      cust.AccountNumber == accountnumber
                select new CustomerRequest
                         {
                           AccountId = cust.AccountId,
                           AccountNumber = cust.AccountNumber,
                           AreaCode = cust.AreaCode,
                           Branch = cust.Branch,
                           BudgetBalance = (decimal) cust.BudgetBalance,
                           BudgetRate = (decimal) cust.BudgetRate,
                           CareOf = cust.CareOf,
                           City = cust.City,
                           CurrentBalance = (decimal) cust.CurrentBalance,
                           CurrentTankPercentage = (decimal) cust.PercentFull, 
};

如果 cust.PercentFull 小于零,我想检查 CurrentTankPercentage 的值是否为零。

我是否必须将其拆开才能对其进行更改?

4

2 回答 2

8

最简单的方法是使用条件运算符:

select new CustomerRequest
 {
   ...
   CurrentTankPercentage = (decimal) (cust.PercentFull < 0 ? 0 : cust.PercentFull) 
 };

另一种方法是使用Math.Max

select new CustomerRequest
 {
   ...
   CurrentTankPercentage = (decimal) Math.Max(cust.PercentFull,0) 
 };

但我觉得这更难阅读。我倾向于将其误读为“给出CurrentTankPercentage最大值 0”,这与它的实际作用相反。

于 2013-06-17T14:49:00.643 回答
1

你可以试试:

CurrentTankPercentage = ((decimal)cust.PercentFull) < 0 ? 0 : ((decimal)cust.PercentFull)
于 2013-06-17T14:50:04.587 回答