微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

您如何在具有父/子关系的 EF 中使用 group by 和 have 子句?

如何解决您如何在具有父/子关系的 EF 中使用 group by 和 have 子句?

如何编写包含 group by 和 having 子句的 linq to entity 查询

例如在 sql 中:

SELECT * FROM dbo.tblParent p
INNER JOIN  
(
    SELECT a.ID
    FROM dbo.tblParent a 
    join dbo.tblChild c ON a.ID = c.FkParentID
    WHERE a.ColValue = 167
    GROUP BY A.ID
    HAVING COUNT(c.ID) = 1
) t ON p.ID = t.ID

解决方法

我找到了自己的答案。

    // this is far from pretty but it works as far as I can tell
Apps = (from x in context.tblParents
                join t in (
                    from p in context.tblParents
                    join c in context.tblChilds
                        on p.ID equals c.FkParentID
                    where p.ColValue  == 167
                    group c.ID by p.ID into grouped
                    where grouped.Count() == 1
                    select new { grouped.Key }) on x.ID equals t.Key
                select x);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。