如何摆脱"If then else“?
If qShoppingCartByCartID.Invoke(_context).Count > 0 Then
Return qShoppingCartByCartID.Invoke(_context).Select(Function(x) x.Products.UnitCost - x.Products.SalePrice).Sum
Else
Return 0
End If发布于 2011-05-12 14:58:33
如果列表为空,则Sum()将返回null -因此您可以只返回sum:
Return qShoppingCartByCartID.Invoke(_context).Select(Function(x) x.Products.UnitCost - x.Products.SalePrice).Sum()在C#中,如果您愿意,还可以使用??将其转换为零而不是null
return qShoppingCartByCartID....Sum() ?? 0;我不是VB用户,但是看起来您可以使用If在VB中做同样的空值合并-请参阅Is there a VB.NET equivalent for C#'s '??' operator?
https://stackoverflow.com/questions/5972438
复制相似问题