假设我有这个SQL:
SELECT p.ParentId, COUNT(c.ChildId)
FROM ParentTable p
LEFT OUTER JOIN ChildTable c ON p.ParentId = c.ChildParentId
where p.IsDeleted=0 and c.IsDeleted=0
GROUP BY p.ParentId
我尝试将其翻译为Linq to Sql
,但我坚持不能在where子句中添加超过1个条件的问题。
from p in context.ParentTable
join c in context.ChildTable on p.ParentId equals c.ChildParentId into j1
where p.IsDeleted = false && c.IsDeleted=false
from j2 in j1.DefaultIfEmpty()
group j2 by p.ParentId into grouped
select new { ParentId = grouped.Key, Count = grouped.Count(t=>t.ChildId != null) }
错误为The name c does not exist in the current context.
我该如何解决这个问题?感谢。
答案 0 :(得分:2)
试试这个
from p in context.ParentTable.Where(x=>x.IsDeleted = false)
join c in context.ChildTable.Where(y=>y.IsDeleted=false) on p.ParentId equals c.ChildParentId into j1
from j2 in j1.DefaultIfEmpty()
group j2 by p.ParentId into grouped
select new { ParentId = grouped.Key, Count = grouped.Count(t=>t.ChildId != null) }