仅返回计数大于X的对象

时间:2013-05-23 04:12:34

标签: c# linq entity-framework

我不确定如何使用实体框架执行此操作。我得到了以下内容:

return this.enrollments
                .Where(e => e.em_enrolled == false && e.em_result < _settings.PassMark)
                .GroupBy(e => e.em_subject_id)
                .Select(e => e.em_subject_id)
                .ToList();

我如何只检索x次出现的记录。

2 个答案:

答案 0 :(得分:7)

您是指具有x个或更多项目的群组吗?

return this.enrollments
                .Where(e => e.em_enrolled == false && e.em_result < _settings.PassMark)
                .GroupBy(e => e.em_subject_id)
                .Where(g => g.Count() >= x)
                .Select(g => g.Key)
                .ToList();

答案 1 :(得分:7)

我怀疑你想要:

return this.enrollments
           .Where(e => !e.em_enrolled && e.em_result < _settings.PassMark)
           .GroupBy(e => e.em_subject_id)
           .Where(g => g.Count() >= x)
           .Select(g => g.Key)
           .ToList();

请注意,我已更改Select部分,以反映您要从组中提取组密钥的事实。 (我也避免与false进行比较,将e.em_enrolled == false更改为!e.em_enrolled。当然,它们的意思相同,但我发现后者在C#中更具惯用性。)