如何参考另一列进行分组和排名

时间:2017-11-08 04:31:51

标签: vb.net linq grouping vb.net-2010 rank

我需要根据主题分组或分区和排名方面的帮助。每个科目都必须拥有头到尾的学生。 我在VB 2010中使用下面的代码。

Dim TotaledRecords = From p In db.Assessments
Where p.Class = cboclass.Text And p.Stream = cbostream.Text
Select p
Order By p.Total Descending

For j = 1 To TotaledRecords.Count
TotaledRecords.ToList(j - 1).Position = j
Next
db.SubmitChanges()

我希望它重新开始关于主题的排名。

见附图:

1 个答案:

答案 0 :(得分:0)

在这种情况下,您应首先按主题排序,然后按总计排序。 像这样:

Order By p.Subject, p.Total Descending

如果你想添加一个按主题分组的排名字段,一种方法就是这样:

rank =(From r In db.Assessments _
     Order By r.Subject, r.Total _
     Where (r.Subject = p.Subject) _
     And (r.Total > p.Total) _
     Select r).Count + 1 _

...而linq查询将是这样的:

Dim TotaledRecords = From p In db.Assessments _
Where p.Class = cboclass.Text And p.Stream = cbostream.Text _
Select p, _
rank =(From r In db.Assessments _
       Order By r.Subject, r.Total _
       Where (r.Subject = p.Subject) _
       And (r.Total > p.Total) _
       Select r).Count + 1 _
Order By p.Subject, p.Total Descending