gpt4 book ai didi

c# - LINQ 等同于 Where 子句中的 SQL IsNull(..,..)

转载 作者:行者123 更新时间:2023-11-30 15:20:42 25 4
gpt4 key购买 nike

在下面的 OUTER JOIN LINQ 查询中,我在 Where 中得到空异常如果右侧行为空(以防 c.CustomerID 与 Outer Join 中的 ord.CustomerID 不匹配)。 问题:如果下面的Where clause中ord.price为null,我该如何处理? 注意:价格是 int? 类型的可为空的列.

Query1 = from c in Customers
join ord in Orders on c.CustomerId equals ord.CustomerId into cord
from t in cord.DefaultIfEmpty()
where t.price = null || t.price > 100
select new {CustName = c.Name, OrderID = (t == null ? 0 : t.OrderId)};

更新:

很抱歉,where 子句中有错别字。

  1. 正如@JeffMercado 指出的那样,应该是t.price并且不是 ord.price
  2. Where 中缺少一个子句条款。我已将其更正为:where t.price = null || t.price > 100 .但现在我收到错误:operator || cannot be applied to operands of type '<null>' and 'int'

最佳答案

你可以这样做:

Query1 = from c in Customers
join ord in Orders on c.CustomerId equals ord.CustomerId into cord
from t in cord.DefaultIfEmpty()
where (ord.price ?? 0) > 100
select new {CustName = c.Name, OrderID = (t == null ? 0 : t.OrderId)};

关于c# - LINQ 等同于 Where 子句中的 SQL IsNull(..,..),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39382660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com