2
Answers

Ranking items in a list with linq

Rich

Rich

12y
7.6k
1
I am trying to figure out a way to rank items in a list, and hold the results in an object or another list.  I know about the orderby method, but I don't exactly know how to go about creating a corresponding rank number for each item. Is this possible with linq?  For example:

 List<int> numbers = new List();

 numbers.Add(650);
 numbers.Add(150);
 numbers.Add(500);
 numbers.Add(200);

and then store the results  from either low to high or high to low here:

public class NumberRank
{
        public int Number {get; set;}
        public int Rank {get; set;}

        public NumberRank(int number)
        {
                Number = number;
                Rank = ????????;
        }
}

Thanks, in advance.
Answers (2)