score:4

Accepted answer

you have basically 2 options here. linq or loop. for both options you should use orderbydescending since your score rank relationship is inverse. then you can use the index + 1 to assign the ranks.

  1. loop.

for this option you need a collection to hold your groupings which can be iterated using the index [ ] operator. this is not possible in iorderedenumerable. so i suggest to use a list:

list<igrouping<int, rankeditem>> rankeditems = items.groupby(b => b.score)
                                                    .orderbydescending(g => g.key)
                                                    .tolist();

now you can simply loop through the list having the index and loop again through all elements of each group to use the index to assign the rank:

for (int i = 0; i < rankeditems.count(); i++)
{
    igrouping<int, rankeditem> grouping = rankeditems[i];
    foreach (var element in grouping)
    {
        element.rank = i + 1;
    }
}
  1. linq:

use the index in this select overload statement and create new objects using your constructor:

list<rankeditem> rankeditems = items.groupby(b => b.score)
                                    .orderbydescending(g => g.key)
                                    .selectmany((item, index) => item.select(inner => 
                                            new rankeditem(inner.name, item.key) {rank = index + 1})
                                    ).tolist();

outcome:

enter image description here


Related Query

More Query from same tag