c# - .Net (C#) 中 ISNULL() 的等效方法是什么?

标签 c# sql entity-framework ado.net

我有一个用于我的购物车系统的存储过程,它返回总金额,如下所示;

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/

相关文章:

c# - 在 .net 中转换期间对象会发生什么变化?

sql - 复合主键+外键

sql - 前 N 个结果分组 Oracle SQL

entity-framework - 如何从命令行指定 dbcontext 的连接字符串

c# - Entity Framework Load() 方法不会加载所有内容

c# - 对 DataSet.Merge 和 RowState 感到困惑

c# - 监听 XAML 中的全局变量变化

c# - Entity Framework 中的自定义显式加载 - 有什么办法吗?

c# - gridview C# 中滚动查看器的水平偏移

mysql - SQL COUNT 没有给我正确的结果