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.
Are you using LINQ to Objects? If so, there’s a
Selectoverload which gives you the index as well as the value. (Ditto forWhereand probably others too.) TheSelectoverload 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?