带有OR条件的c# Lambda Join



是否可以将此SQL语句转换为c# lambda语法?

SELECT *
FROM Table1 t1
INNER JOIN Table2 t2 on t1.Table1_ID = t2.Table1_ID
OR t1.Some_Other_Column = NULL
对于c#语法,我会这样做:
context.Table1
.Join(context.Table2,
t1 => t1.Table1_ID,
t2 => t2.Table1_ID,
(t1, t2) => new { t1, t2 }
.Select(x => new 
{
// start listing out properties here:
x.t1.first_column,
x.t1.last_column,
x.t2.first_column,
// and so on
}).ToList();

我只是不确定如何包括条件:OR t1。Some_Other_Column = NULL变成上面的语句。如有任何帮助,不胜感激。

不能使用JoinLINQ运算符,因为它只支持连接表达式的相等性。

你可以通过where操作来做到这一点,LINQ提供程序如何转换它将取决于提供程序:

source1.SelectMany(x => source2.Select(y => new { x, y })
.Where(v => v.x.Prop1 = v.y.Prop1
|| v.x.Prop2 is null)

最新更新