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

c# – Sql到Linq难度 – 分组,有

我在sql中有以下查询,我想将其转换为LINQ:
select profile_id from t
where child_id in (1,2,3,...) //this will be a list of integers CHILDREN
group by profile_id
having count(distinct child_id) = 3

我有一个困难,如何将我的SQL查询中的最后一行写入linq.以下是我到目前为止的工作:

public IQueryable<ProfileChildRelationship> GetPCRelByCids(List<int> children)
    {
        var query = from pcr in this._entities.ProfileChildRelationships
                    where children.Contains(pcr.pcChildIDF)
                    group pcr by pcr.pcProfileIDF into g
                    ??? having ...?
                    select pcr;

        return query;
    }

我认为可能主要的问题是,许多人将sql语句转换为where linq语句,但在我的情况下,我不认为有可能在linq语句组之后写另一个地方!

更新:

情况:我有很多孩子,每个孩子都有很多不同的个人资料(有些可能是相同的).用户将选择一些子项,我希望从中获取它们的公共配置文件.也就是说,如果为每个孩子找到了配置文件X,那么,如果找到除了一个孩子之外的每个孩子的配置文件Y,那么它将无效!

解决方法

听起来你想在这里使用where子句……
var query = from pcr in this._entities.ProfileChildRelationships
            where children.Contains(pcr.pcChildIDF)
            group pcr by pcr.pcProfileIDF into g
            where g.Select(x => x.ChildId).distinct().Count() == 3
            select g.Key; // This is the profile ID

原文地址:https://www.jb51.cc/csharp/91565.html

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

相关推荐