score:4
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.
- 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;
}
}
- 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:
Source: stackoverflow.com
Related Query
- Linq for calculating rank with group and orderby
- How linq function OrderByDescending and OrderBy for string length works internally? Is it faster than doing it with loop?
- Stubbing Code for Test With Linq Expressions and Lambdas
- LINQ to Entities query for count and group by with join on 2 tables
- Best practices for dealing with LINQ statements that result in empty sequences and the like?
- Linq syntax for OrderBy with custom Comparer<T>
- Entity Framework - Linq query with order by and group by
- OrderBy and Top in LINQ with good performance
- LINQ Query with GROUP and SUM
- LINQ - writing a query with distinct and orderby
- Simple sql to Linq query with group by and aggregate functions
- Translating query with GROUP BY and COUNT to Linq
- LINQ query with GROUP BY and Count(*) into Anonymous Type
- Linq OrderBy to group objects with the same sets
- Opensource C++ LINQ library with dot notation, orderBy and firstOrDefault?
- LINQ with Lambda expression - Join, Group By, Sum and Count
- LINQ with GROUP BY and HAVING COUNT
- How can I group by the difference between rows in a column with linq and c#?
- LINQ - Writing an extension method to get the row with maximum value for each group
- LINQ with Group By and Having Clause with Min(string)
- Group items and select specific item from each group with LINQ
- Get latest record and group with highest date - LINQ
- Optimize/rewrite LINQ query with GROUP BY and COUNT
- Linq check for null and replace null value in orderby
- Convert Datatable to Object with Linq and Group by
- LINQ In Clause With Group By And TOP N Records Within Each Group
- What are the prerequisites for using SQL and LINQ with my winforms application in C#?
- Linq SQL error with one-to-many relationship and orderby complex expression
- C# - Linq optimize code with List and Where clause
- C# Linq Join 2 tables on multiple columns and GROUP BY for count
More Query from same tag
- How to convert all date-formatted cells in IRange to DateTime using Spreadsheet Gear C#?
- Is this de best way to update an entity from DataContext?
- Get count of a datatable column contains with specific value
- linq to entities - try this one!
- TransactionScope vs Transaction in LINQ to SQL
- How does entity framework access associations?
- Do Compiled Queries Cache?
- Linq to DataSet Order By Clause Error
- Add,delete or update a Object with children entities
- C# listbox Collection syntax
- Using a member access lambda expression to parametrise a LINQ to SQL predicate
- Can my two linq lines of code be combined?
- ASP.NET Web API failed to serialize the response content
- How to navigate navigation properties using Linq and a repository pattern?
- C# Linq Intersect Hashset<T> in memory with IEnumerable<T> from filestream
- LinqToTwitter List DMs since a specified date
- How do I cache compiled LINQ query (not results)?
- Return those records off some parents from which their children's certain property equals a certain value?
- Linq with date comparision not working
- Order by a field (int). If the field it is not int?
- Entity Framework with Condition on SaveChanges
- Linq Remove results with ALL statement
- Can this be refactored into nicey nice LINQ?
- linq ordered subset of another list
- How to return IList from my repository with ordered queries in LINQ/EF
- LINQ Join using dot notation and more than one joining field
- List in Linq where condition
- Aggregation on Data with C# or SQL
- C# how to write JSON with enumerated identifiers
- How-to ensure multiple collections have the same item count using LINQ