score:1
You are close to achieve what you want. Here is my take on what I believe you are trying to do.
If the amount of data is small it doesn't matter that you are using GroupBy
multiple times but it might be better to create the groups only once:
var groupings = allData
.GroupBy(
score => score.Player,
(player, scores) => new
{
Player = player,
Scores = scores.ToList()
})
.ToList();
Calling ToList
will ensure that the collection of scores for each player are enumerated and allocated into lists.
I'm not sure about the part where you determine the minimum number of records. It looks odd to compute the minimum of the Player
property which I assume is the name of the player.
Here I compute the minimum number of scores for all players:
var minScoreCount = groupings.Min(grouping => grouping.Scores.Count);
In the last part you have to use Take(minScoreCount)
to only take the first N scores for each player. I think this is the part that you are missing in your own code.
var sumData = groupings.Select(grouping => new Score
{
Player = grouping.Player,
Points = grouping.Scores
.OrderByDescending(score => score.Points)
.Take(minScoreCount)
.Sum(score => score.Points),
Strokes = grouping.Scores
.OrderBy(score => score.Strokes)
.Take(minScoreCount)
.Sum(score => score.Strokes)
}).ToList();
If you didn't have the requirement to sort the scores differently for Points
and Strokes
you could sort the scores before the ToList
inside the GroupBy
reducing the number of times the code is traversing the lists of scores.
score:1
Not sure how pretty it is, but here you go. You can do it like this:
[ActionName("List")]
public async Task<IActionResult> List()
{
var allData = await _cosmosDbService.GetScoresAsync(
"SELECT * FROM c");
var playerRounds = allData.GroupBy(l => l.Player);
var minRounds = playerRounds.Min(x => x.Count());
var sumData = playerRounds.Select(cl => new Score
{
Player = cl.First().Player,
Points = cl.OrderByDescending(x => x.Points).Take(minRounds).Sum(c => c.Points),
Strokes = cl.OrderByDescending(x => x.Points).Take(minRounds).Sum(c => c.Points),
}).ToList();
return View(sumData);
}
Source: stackoverflow.com
Related Articles
- How can I group on a specific amount of rows using LinQ?
- Read all rows of a specific column using LINQ
- Using Linq Grouping how to I order by specific Group then remaining Groups descending
- How to sum the values of a specific column attribute of individual rows returned using a LINQ expression? (C#)
- Extract Specific Rows from a Database using LINQ
- Group the rows in a datatable using linq
- Getting a amount of rows with same month with LINQ from an MVC model using dateTime, is it possible?
- LINQ get rows from a table that don't exist in another table when using group by?
- Get the specific count of each column using GROUP BY LINQ
- Group rows with identical value into column using LINQ
- using LINQ to delete specific rows from DataTable
- Joining specific 2 rows of existing list using linq
- Creating a datatable with only the rows that match a specific column name prefix using Linq in c#
- Copy specific rows from One DataTable to another DataTable using LINQ
- datatable sum column and concatenate rows using LINQ and group by on multiple columns
- add all rows of multiple tables into one and group using linq
- Display specific rows and update in GridView using Linq database c#
- Converting Datatable to sublist using linq - retrieve rows without group by key
- How to load xml code block into existing xml file at a specific node using linq
- Convert string[] to int[] in one line of code using LINQ
- Using Linq to group a list of objects into a new grouped list of list of objects
- How to get first record in each group using Linq
- how to update the multiple rows at a time using linq to sql?
- LINQ to SQL using GROUP BY and COUNT(DISTINCT)
- Return list of specific property of object using linq
- Populate a list with a specific range of numbers by using LINQ
- Counting Using Group By Linq
- Using LINQ to group a list of objects
- How to use Linq to group every N number of rows
- Using LINQ to group by multiple properties and sum
- How would you do a "not in" query with LINQ?
- Unique Sub-JSon object of a Json object in C#
- Check string pattern in LINQ query
- LINQ Partition List into Lists of 8 members
- How to Moq the "Expression<Func<T, TType>>"
- Linq very slow query (never completed)
- How to write Linq expression using external objects
- Cleaner way to convert collection into another type?
- search in ICollection entity
- How to convert datatable record into child custom Class
- How to combine more than two generic lists in C# Zip?
- Dynamic LINQ with Data Objects
- Creating a hierarchical structure from a list of sequence strings
- How to dynamically order by certain entity properties in Entity Framework 7 (Core)
- Get distinct list objects from list using LinQ
- LINQ to JSON - Select Individual properties belong to a navigational property
- Convert a MySQL statement containing where, group by, sum and having to Linq to Sql
- Im facing Big performance impact on IEnumerable(LINQ) > List(RecyclerView)
- Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1>' to 'System.Linq.IQueryable<>'
- Change lambda expression to a method - LINQ