gpt4 book ai didi

c# - 将 SQL 查询转换为 LINQ -- 运算符 '&&' 不能应用于类型 'int' 和 'bool' 的操作数

转载 作者:太空宇宙 更新时间:2023-11-03 18:23:18 27 4
gpt4 key购买 nike

我在将 SQL 查询转换为 LINQ 时遇到问题。我认为我已将其 95% 复制到 LINQ 中,但在连接时遇到问题

SQL

SELECT 
table1.Column1
FROM
table 1 table1
LEFT OUTER JOIN
table 2 table2 ON table2.Column1 = table1.Column1
AND table2.Column2 = 1838
WHERE
table1.Column2 = 1
AND table1.Column3 = 24029
AND (table2.[Column3] IS NULL OR table2.[Column3] = 1)

到目前为止的 LINQ

var query = from table1 in table 1
join table2 in table 2 on table1.Column1 equals table2.Column1 && table2.Column2 == 1838 into result
from table2 in result.DefaultIfEmpty()
where table1.Column2 == 1 && table1.Column3 == 24029 && (table2.Column3 == null || table2.Column3 == 1)
select table1.Column1;

当我运行查询时,我在这一行收到错误

join table2 in table 2 on table1.Column1 equals table2.Column1 && tabl2.Column2 == 1838 into result

错误:

CS0019 Operator '&&' cannot be applied to operands of type 'int' and 'bool'

有什么建议吗?

最佳答案

composite key join 的 LINQ 语法是不同的。所以而不是不正确

join table2 in table_2 on table1.Column1 equals table2.Column1 && table2.Column2 == 1838 into result

你可以用这样的东西

join table2 in table_2
on new { table1.Column1, Column2 = 1838 } equals new { table2.Column1, table2.Column2 }
into result

关于c# - 将 SQL 查询转换为 LINQ -- 运算符 '&&' 不能应用于类型 'int' 和 'bool' 的操作数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43393387/

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