如何在查询中更改值

Ero*_*ocM 0 c# linq

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

        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, 
};
Run Code Online (Sandbox Code Playgroud)

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

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

D S*_*ley 8

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

select new CustomerRequest
 {
   ...
   CurrentTankPercentage = (decimal) (cust.PercentFull < 0 ? 0 : cust.PercentFull) 
 };
Run Code Online (Sandbox Code Playgroud)

另一种方法是使用Math.Max:

select new CustomerRequest
 {
   ...
   CurrentTankPercentage = (decimal) Math.Max(cust.PercentFull,0) 
 };
Run Code Online (Sandbox Code Playgroud)

但我发现更难阅读.我倾向于误读为"给出CurrentTankPercentage最大值0",这与它的确实相反.