Monday, April 11, 2011

How to get the index of a record ordered by something in Linq?

Hi,

Assume, I have student exam result set which is ordered by a point. I want to get the order index of a student in this result set. How can I get rid of that by using linq?

Assume the class like this;

class ExamResult
{
   int StudentId;
   int Point;
   int ExamId;
}

ExamId StudentId Point
 1        1       5,2
 1        2       5,4
 1        3       3
 1        4       7,2
 1        5       7,9
 1        6       1
 1        7       2,6
 1        8       6
 1        9       11

After I ordered the students by point. Student 2 has to be at index 5.

From stackoverflow
  • Are you using LINQ to Objects? If so, there's a Select overload which gives you the index as well as the value. (Ditto for Where and probably others too.) The Select overload therefore makes it easy to transform the original value into "original value + index" as an anonymous type.

    Your question wasn't entirely clear - if this wasn't what you're after, perhaps you could give some sample code?

0 comments:

Post a Comment