C# 如何将Rownum添加到GroupBy Linq
我有一个复杂的LINQ查询来抽取我所在大学的优秀学生。以下是查询:C# 如何将Rownum添加到GroupBy Linq,c#,linq,C#,Linq,我有一个复杂的LINQ查询来抽取我所在大学的优秀学生。以下是查询: var query = Db.Students.AsNoTracking().Where(...).AsQueryable(); var resultgroup = query.GroupBy(st => new { st.Student.CourseStudyId,
var query = Db.Students.AsNoTracking().Where(...).AsQueryable();
var resultgroup = query.GroupBy(st => new
{
st.Student.CourseStudyId,
st.Student.EntranceTermId,
st.Student.StudyingModeId,
st.Student.StudyLevelId
}, (key, g) => new
{
CourseStudyId = key.CourseStudyId,
EntranceTermId = key.EntranceTermId,
StudyingModeId = key.StudyingModeId,
StudyLevelId = key.StudyLevelId,
list = g.OrderByDescending(x =>
x.StudentTermSummary.TotalAverageTillTerm).Take(topStudentNumber)
}).SelectMany(q => q.list).AsQueryable();
这个查询根据4个参数和他们的总平均成绩给了我前n名学生
现在我想为每个组添加rownum以模拟总排名,例如输出为:
现在我想添加TotalRank作为行数,就像Sql一样。图中X1=1,X2=2,X3=3,Y1=1,Y2=2,Y3=3
如果我想减少这个问题。我只在一个小组工作。代码如下:
resultgroup = query.GroupBy(st => new
{
st.Student.StudyLevelId
}, st => st, (key, g) => new
{
StudyLevelId = key.StudyLevelId,
list = g.OrderByDescending(x =>
x.StudentTermSummary.TotalAverageTillTerm)
.Take(topStudentNumber)
}).SelectMany(q => q.list).AsQueryable();
你是说:
var query = Db.Students.AsNoTracking().Where(...).AsQueryable();
var resultgroup = query.GroupBy(st => new
{
st.Student.CourseStudyId,
st.Student.EntranceTermId,
st.Student.StudyingModeId,
st.Student.StudyLevelId
}, (key, g) => new
{
CourseStudyId = key.CourseStudyId,
EntranceTermId = key.EntranceTermId,
StudyingModeId = key.StudyingModeId,
StudyLevelId = key.StudyLevelId,
list = g.OrderByDescending(x =>
x.StudentTermSummary.TotalAverageTillTerm)
.Take(topStudentNumber)
.Select((x, i) => new { Item = x, TotalRank = i /* item number inside group */}),
StudentsInGroupCount = g.Count() // count group this items
}).SelectMany(q => q).AsQueryable();
要查看结果,请执行以下操作:
foreach (var item in resultgroup.ToList())
{
item.list.ForEach(s => Console.WriteLine(s.TotalRank));
}
list是一个student的列表,但我没有看到student具有rank属性的迹象,所以我将其包装为带有rank的注释性类型
var query = Db.Students.AsNoTracking().Where(...).AsEnumerable();
var resultgroup = query.GroupBy(st => new {
st.Student.CourseStudyId,
st.Student.EntranceTermId,
st.Student.StudyingModeId,
st.Student.StudyLevelId
})
.SelectMany( g =>
g.OrderByDescending(x =>x.StudentTermSummary.TotalAverageTillTerm)
.Take(topStudentNumber)
.Select((x,i) => new {
CourseStudyId = g.Key.CourseStudyId,
EntranceTermId = g.Key.EntranceTermId,
StudyingModeId = g.Key.StudyingModeId,
StudyLevelId = g.Key.StudyLevelId,
Rank = i+1
//studentPorperty = x.Prop1,
})
)
.AsQueryable();
似乎您想要TotalRank=g.Count?TotalRank不是列表+1中的人的索引吗:list=g.OrderByDescendingx=>x.StudentTermSummary.TotalAverageTillTerm.TaketopStudentNumber.Selectx,i=>new{prop1=x.prop1,rank=i+1}可能有帮助。尤其是最小部分。如果你把问题简化为一组学生,那么问题就会简化为在linq中添加一个行号,对吗?如果是这样的话,这将是一个很好的选择,我尝试选择x,但C上升错误如果我添加总排名,我在哪里可以使用它?如何在SelectMany中使用它?请尝试以下操作:.SelectManyq=>q.AsQueryable;所以你有你想要做的所有信息,但它只给我每个组的排名。例如,student1,2,3排名1,student4,5,6=rank2…Select index list=g.orderbydowningx=>x.StudentTermSummary.TotalAverageTillTerm.TaketopStudentNumber.Selectx,i=>new{item,i}它给我错误:方法的类型参数。。。。无法从用法推断。试着用explicitlyi指定类型参数。如果这样,我们都可以承认这个问题是重复的。我们根本无法直接解决这个问题。嘿,你的答案给我每个小组的排数。我不想要它。我想在每个组中添加rownum。也就是说,如果我有一组3个学生,它会给我1,2,3。但是您的回答对此组只给出1如果我在resultgroup{print item.List,item.rank}中使用foreach var item,您知道这是错误的item,因为子列表是您的设计选择。顺便说一句,因此没有item.List,item.rank,如果您丢失了,请使用自动竞争或调试来探索该项目。我已经为我认为更容易的事奉承了选择。如果不适合,请使用“编辑历史记录”。顺便说一句,我出去了。请参考和。从30分钟以来,它一直在我这边工作。“这不是我的工作。”mahdimoghimi,看看顶部的AsQueryable,把它换成。AsEnumerable。不知道AsQueryable在那里做什么,但是enumerable可以加载学生并在Memory中进行分组。无需将项目代码转换为SQL。