Converting SQL Rank() to LINQ, or alternative

前端 未结 4 1988
野趣味
野趣味 2020-12-06 06:00

I have the below SQL statement that works as desired/expected. However I would like to translate it into a LINQ statement(Lambda??) so that it will fit with the rest of my

4条回答
  •  情歌与酒
    2020-12-06 06:32

    LINQ has rank funcionality built in, but not in the query syntax. When using the method syntax most linq functions come in two versions - the normal one and one with a rank supplied.

    A simple example selecting only every other student and then adding the index in the resulting sequence to the result:

    var q = class.student.OrderBy(s => s.studentId).Where((s, i) => i % 2 == 0)
    .Select((s,i) => new
    {
      Name = s.Name,
      Rank = i
    }
    

提交回复
热议问题