4

我试过

db.Table1.GroupJoin(db.Table2,
    t1 => t1.Id,
    t2 => t2.t1Id,
    (t1,g) => new { t1, g })
  .Where(item => !item.g.Any())
  .Select(item => item.t1);

但它返回了 0 个结果。我正在尝试编写一些生成以下 SQL 代码(或类似代码)的东西

SELECT t1.*
FROM Table1 as t1
LEFT OUTER JOIN Table2 as t2
ON t1.Id = t2.t1Id
WHERE IsNull(t2.Id);
4

1 回答 1

3

看起来您想要的结果是识别Table1在左外连接期间没有结果的项目Table2

用流利的符号表示,该方法类似于以下内容:

var query = db.Table1.GroupJoin(db.Table2,
                t1 => t1.Id,
                t2 => t2.t1Id,
                (t1, joined) => new { t1, joined }
            )
            .SelectMany(r => r.joined.DefaultIfEmpty(), (r, j) => new  
            {
                r.t1, 
                j
            })
            .Where(r => r.j == null)
            .Select(r => r.t1);

在查询语法中:

var query = from t1 in db.Table1
            join t2 in db.Table2 on t1.Id equals t2.t1Id into joined
            from j in joined.DefaultIfEmpty()
            where j == null
            select t1;
于 2011-06-04T20:42:26.780 回答