sql具有不同字段数的语句

时间:2018-01-15 01:29:13

标签: sql linq count distinct having

我需要你的帮助 我怎样才能将这个sql解释为linq:

SELECT  FK_ClassId, LectureDays
FROM tbl_TimeTables
WHERE (FK_ClassId = 11)
group by fk_classid, lecturedays
having count(distinct Period) < 7

非常感谢任何帮助 添

2 个答案:

答案 0 :(得分:0)

应该是这些方面的东西 (按ID分组,计算期间,适用于where子句,项目结果)

var query = from timetable in context.TimeTables
            where timetable.ClassId == classId
            group timetable by new
            {
                timetable.ClassId,
                timetable.Period
            } into groupings
            let counter = groupings
                .Select(p => p.Period)
                .Distinct()
                .Count()
            where counter < 7
            select new
            {
                ClassId = groupings.Key.ClassId,
                Days = from grouping in groupings
                       select grouping.LectureDays
            };

答案 1 :(得分:0)

谢谢你把我指向了正确的方向,对不起,我现在还没回复一段时间,被捆绑了。 在看了你的解决方案后,我有时间玩它,下面就为我做了这个:

from t in Tbl_TimeTables 
where t.FK_ClassId == 11
        group t by new
        {
            t.FK_ClassId,
            t.LectureDays
        } into groupings
        let counter = groupings
            .Select(p => p.Period)
            .Distinct()
            .Count()
        where counter < 7
        select new
        {
            ClassId = groupings.Key.FK_ClassId,
            Days = (from grouping in groupings
                   select grouping.LectureDays).Distinct()
        }

我只需要通过键将组更改为lecturedays而不是period,这会删除我得到的一系列结果。 非常感谢Dan