简体   繁体   English

Linq到实体

[英]Linq to Entities

How to get rid of "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

If the list is empty then Sum() will return null - so you could just return the sum: 如果列表为空,则Sum()将返回null因此您可以返回总和:

  Return qShoppingCartByCartID.Invoke(_context).Select(Function(x) x.Products.UnitCost - x.Products.SalePrice).Sum()

In C# you could also use ?? 在C#中,您还可以使用?? to turn that into a zero instead of null if you wanted to 如果您想将其变为零而不是null

  return qShoppingCartByCartID....Sum() ?? 0;

I'm not a VB user, but it looks like you can do this same null coallescing in VB using If - see Is there a VB.NET equivalent for C#'s '??' 我不是VB用户,但看起来您可以使用If在VB中执行相同的空合并-请参阅是否有C#的??等价于VB.NET的等效项? operator? 操作员?

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

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