作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我在 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/
我是一名优秀的程序员,十分优秀!