gpt4 book ai didi

c# - Sql to Linq 难点——group by, having

转载 作者:太空狗 更新时间:2023-10-29 21:13:51 26 4
gpt4 key购买 nike

我在 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;
}

我认为这可能的主要问题是许多人将 having sql 语句转换为 where linq 语句,但就我而言,我认为不可能在 group by linq 语句之后编写另一个 where!

更新:

情况:我有很多 child ,每个 child 都有很多不同的个人资料(有些可能相同)。用户将选择一些 child ,我想从中得出他们的共同配置文件。也就是说,如果为每个 child 都找到配置文件 X,那么我会得到它,如果为除了一个 child 之外的每个 child 都找到配置文件 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

关于c# - Sql to Linq 难点——group by, having,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10398392/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com