我有一个用于我的购物车系统的存储过程,它返回总金额,如下所示;
ALTER PROCEDURE [dbo].[ShoppingCartGetTotalAmount]
(@CartID char(36))
AS
SELECT ISNULL(SUM(Product.Price * ShoppingCart.Quantity), 0)
FROM ShoppingCart INNER JOIN Product
ON ShoppingCart.ProductID = Product.ProductID
WHERE ShoppingCart.CartID = @CartID
但是,现在我想在 Entity Framework 中做同样的事情。因此,我需要知道以下任一选项;
1) 如何在 Entity FrameWork i-e 中完成上述任务
SELECT ISNULL(SUM(Product.Price * ShoppingCart.Quantity), 0)
FROM ShoppingCart INNER JOIN Product
ON ShoppingCart.ProductID = Product.ProductID
WHERE ShoppingCart.CartID = @CartID
2) 或者 C# 中 SQL ISNULL() 函数的等价物是什么?
3) 或者我怎样才能实现这个 -> ISNULL(SUM(Product.Price * ShoppingCart.Quantity), 0)
使用任何 .Net 方法?
最佳答案
C# 有 null coalesce operator - ??
。
The ?? operator is called the null-coalescing operator and is used to define a default value for nullable value types or reference types. It returns the left-hand operand if the operand is not null; otherwise it returns the right operand.
EF 理解此运算符并正确翻译它。
关于c# - .Net (C#) 中 ISNULL() 的等效方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12238561/