我已经让外连接在Oracle中正常工作,但在转换为LINQ时无法工作。当通过LINQ执行时,我没有记录,因为对于搜索到的用户,记录在T_PER_ADDL_ATRB中不存在
SQL
SELECT P.*
FROM T_PER P
INNER JOIN T_PER_CMN_ATRB C ON C.PER_ID = P.PER_ID
LEFT OUTER JOIN T_PER_ADDL_ATRB A22 ON A22.PER_CMN_ATRB_ID = C.PER_CMN_ATRB_ID
WHERE A22.ADDL_ATRB_ID = 22
AND (C.PER_LST_NM LIKE 'Chen%' or A22.ADDL_ATRB_VAL like 'Chen%');
SQL返回1条记录
LINQ
(from P in db.T_PER
join C in db.T_PER_CMN_ATRB on P.PER_ID equals C.PER_ID
join A22 in db.T_PER_ADDL_ATRB on C.PER_CMN_ATRB_ID equals A22.PER_CMN_ATRB_ID into A22_join
from A22 in A22_join.DefaultIfEmpty()
where
A22.ADDL_ATRB_ID == 22 &&
(C.PER_LST_NM.StartsWith("Chen") ||
A22.ADDL_ATRB_VAL.StartsWith("Chen"))
select P).ToList()
LINQ返回0条记录
答案 0 :(得分:1)
public static IEnumerable<TResult> LeftJoin<TSource, TInner, TKey, TResult>(this IEnumerable<TSource> source, IEnumerable<TInner> inner, Func<TSource, TKey> pk, Func<TInner, TKey> fk, Func<TSource, TInner, TResult> result)
{
IEnumerable<TResult> _result = Enumerable.Empty<TResult>();
_result = from s in source
join i in inner
on pk(s) equals fk(i) into joinData
from left in joinData.DefaultIfEmpty()
select result(s, left);
return _result;
}
答案 1 :(得分:0)
将Where
与DefaultIfEmpty()
一起使用,以便在LEFT OUTER JOIN
子句中添加条件
(from P in db.T_PER
join C in db.T_PER_CMN_ATRB on P.PER_ID equals C.PER_ID
join A22 in db.T_PER_ADDL_ATRB on C.PER_CMN_ATRB_ID equals A22.PER_CMN_ATRB_ID into A22_join
from A22 in A22_join.Where(x => x.ADDL_ATRB_ID == 22).DefaultIfEmpty()
where
(C.PER_LST_NM.StartsWith("Konyayev") ||
(A22 == null || (A22 != null && A22.ADDL_ATRB_VAL.StartsWith("Konyayev"))
)
)
select P).ToList()
答案 2 :(得分:-1)
您可以使用类似下面的内容:
from p in db.TPER
join c in db.T_PER_CMN_ATRB on p.PER_ID equals c.PER_ID
join a22 in db.T_PER_ADDL_ATRB.Where(p=> p.ADDL_ATRB_ID == 22) on c.PER_ID equals a22.PER_CMN_ATRB_ID into a22_join
from a22table in a22_join.DefaultIfEmpty()
select new {
a22table.ADDL_ATRB_ID,
C.PER_LST_NM,
A22.ADDL_ATRB_VAL
P,
}).Where(p=> p.PER_LST_NM.StartsWith("Chen") || p.ADDL_ATRB_VAL.StartsWith("Chen")).ToList()