gpt4 book ai didi

c# - 使用三元运算符检查 null - null 引用是否与 null 不同

转载 作者:行者123 更新时间:2023-11-30 21:02:04 28 4
gpt4 key购买 nike

我正在尝试使用三元运算符来检查值是否为 null 并返回一个表达式或另一个。将此合并到 LINQ 表达式时,我遇到的是 LINQ 表达式的 Transact-SQL 转换试图执行“column = null”而不是“column IS NULL”。我有理由相信这是因为我正在执行以下操作:

mappedColumnName == (myVar == null ? null : myOtherVar)

因为它将以下内容转换为 Transact-SQL 中的 columnName IS NULL:

mappedColumnName == null

有没有人有这方面的经验?我非常想让它发挥作用。

整个 LINQ 表达式:

(from MenuItem in menuContext.Menus
where MenuItem.IsSysAdmin == (ClientID == 1 ? true : false)
&& MenuItem.IsActive == true
&& MenuItem.ParentMenuCode == (ActiveSubMenu==null?null:ActiveMenu)
&& MenuItem.ClientID == (UseClientMenu ? ClientID : 0)
&& MenuItem.EmployeeID == (UseEmployeeMenu ? EmployeeID : 0)
orderby MenuItem.SortOrder, MenuItem.MenuName
select MenuItem);

最佳答案

为什么不用

mappedColumnName == (myVar == null ? DBNull.Value: myOtherVar)

代替?

关于c# - 使用三元运算符检查 null - null 引用是否与 null 不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13700460/

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