我在T-SQL
中有一个查询,该查询包含按子句划分的分区,并且基于生成的ROWNumber,我想进行两件事:
这是我想转换为EF的T-SQL查询:
select * from
(
select StudentId, ProgramId, StudentProgramTaggingId,
ROW_NUMBER() OVER(Partition By StudentId, ProgramId order by StudentProgramTaggingId) as RowNum
from MahadStudentProgramHistory
group by StudentId, ProgramId, StudentProgramTaggingId
)a where a.RowNum > 1
这是我在EF中尝试获得所需结果的方法:
var studentPrograms = db.StudentProgramHistories;
var groupByStudents = studentPrograms
.GroupBy(x => new
{
x.StudentId,
x.ProgramId
})
.Select(x => x.Count() > 1)
.Select(x => x).ToList();
上面的EF代码提取跳过重复记录的所有记录,但是我想相反,我想从重复记录中获取第一条记录,并忽略所有单身的订单。
答案 0 :(得分:0)
我尝试了此代码及其工作。也许由于ToList函数,您不想使用它。
var studentPrograms = db.StudentProgramHistories
.ToList() // this may slow
.Select((s, i) => new { Row = s, Idx = i + 1 })
.Where(w => w.Idx > 1)
.Select(s => s.Row)
.ToList();