关于C#:linq-左联接、分组依据和计数

LINQ - Left Join, Group By, and Count

假设我有这个SQL:

1
2
3
4
SELECT p.ParentId, COUNT(c.ChildId)
FROM ParentTable p
  LEFT OUTER JOIN ChildTable c ON p.ParentId = c.ChildParentId
GROUP BY p.ParentId

如何将其转换为LINQ到SQL?我陷入了计数(c.childid)的困境,生成的SQL似乎总是输出count(*)。到目前为止,我得到的是:

1
2
3
4
5
from p in context.ParentTable
join c in context.ChildTable 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() }

谢谢您!


1
2
3
4
5
from p in context.ParentTable
join c in context.ChildTable 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) }


Consider using a subquery:

1
2
3
4
5
6
7
8
from p in context.ParentTable
let cCount =
(
  from c in context.ChildTable
  where p.ParentId == c.ChildParentId
  select c
).Count()
select new { ParentId = p.Key, Count = cCount } ;

If the query types are connected by an association,this简化to:

1
2
3
from p in context.ParentTable
let cCount = p.Children.Count()
select new { ParentId = p.Key, Count = cCount } ;


晚回答:

如果你所做的一切都是计数注意到join...into是实际转换到GroupJoin的回归群体,如new{parent,IEnumerable},所以你只需要在小组上打电话给Count()

1
2
3
from p in context.ParentTable
join c in context.ChildTable on p.ParentId equals c.ChildParentId into g
select new { ParentId = p.Id, Count = g.Count() }

在扩展方法中,join into等于GroupJoin(While a join),没有intojoin

1
2
3
4
5
6
7
context.ParentTable
    .GroupJoin(
                   inner: context.ChildTable
        outerKeySelector: parent => parent.ParentId,
        innerKeySelector: child => child.ParentId,
          resultSelector: (parent, children) => new { parent.Id, Count = children.Count() }
    );


当连接语法的概念是模仿SQL语法时,你不应该总是想到直接将SQL代码翻译成Linq。在这一具体情况下,我们不需要加入小组,因为我们加入了一个小组。

这是我的解决办法

1
2
3
from p in context.ParentTable
join c in context.ChildTable on p.ParentId equals c.ChildParentId into joined
select new { ParentId = p.ParentId, Count = joined.Count() }

不如在这里最投票的解决办法,我们不需要J1,J2和Null checking in count(t=>t.childid!=NULL)


ZZU1