无法使用左连接和组向 where 添加多个条件
Can not add multiple conditions to where with left join and group
假设我有这个 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.
我该如何解决这个问题?谢谢
试试这个
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) }
假设我有这个 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.
我该如何解决这个问题?谢谢
试试这个
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) }