我试图在2个参数上做一个Left Out Join,但是失败了。
基本上,我需要的是
DECLARE @SomeValue
SET @SomeValue = 99
SELECT * FROM
[Table1] LEFT OUTER JOIN [Table2]
ON ([Table1].[Field1]=[Table2].[Field1] AND [Table2].[Field2]=@SomeValue)
如何使用LINQ执行此操作?
答案 0 :(得分:2)
第二次......不确定我是否完全喜欢这种解决方案 - 但有一种方法是:
var someValue = 99;
var preQuery = from t2 in Table2
where t2.Field2 == someValue
select t2;
var query = from t1 in Table1
join t2 in preQuery on t1.Field1 equals t2.Field1
select new
{
T1 = t1,
T2 = t2.FirstOrDefault()
};
答案 1 :(得分:1)
LINQ to SQL:
int someValue = 99;
var query =
from table1 from dc.Table1Items
join table2 from dc.Table2Items
on new { table1.Field1, Field2 = someValue } equals new { table2.Field1, table2.Field2 } into table2Items
from table2 in table2Items.DefaultIfEmpty()
select new
{
table1,
table2,
};
答案 2 :(得分:1)
var someValue = 99;
var query = from t1 in Table1
join t2 in Table2
on t1.Field1 equals t2.Field1 into tempTable
from t3 in tempTable.Where(v => v.Field2 == someValue).DefaultIfEmpty()
select new { t1, t3};
答案 3 :(得分:1)
这是我最喜欢的左外连接语法:
from t1 in Table1
from t2 in Table2
.Where(x => x.Field1 == t1.Field1 &&
x.Field2 == someValue)
.DefaultIfEmpty()
select new { t, t2 }
答案 4 :(得分:0)
假设你想在Linq中使用它:
var someValue = 99;
var qry = from t1 in Table1
join t2 in Table2 on t1.Field1 equals t2.Field1 into tempTable
from t3 in tempTable.FirstOrDefault()
where (t3==null || t3.Field2 == someValue)
select new
{
T1 = t1,
T2 = t3
};
答案 5 :(得分:0)
var query = from t1 in table1 join t2 in table2 equals t2.field2 = @somevalue into new table
from t in table.DefaultIfEmpty() select new { t1, t2== null ? t1.field : t2.field };