我有一个 List<T>
已经用 SQL 数据库中的数据填充的对象/类。我想做的是找出这张表中最常见的用户是谁,并根据最常见的用户开始对新列表进行排序
我不知道如何在 Stackoverflow 上设置表格样式,因此您必须耐心等待。
例如数据库表可以是这样的:
id - userID - randomColumn - randomColumn2
1 - 2 - ExampleText - ExampleText
2 - 2 - ExampleText - ExampleText
3 - 1 - ExampleText - ExampleText
4 - 3 - ExampleText - ExampleText
5 - 2 - ExampleText - ExampleText
6 - 1 - ExampleText - ExampleText
我想使用 LINQ 或 SQL 来格式化它,所以我得到一个看起来像这样的列表
userID - amountColumn - randomColumn
2 - 3 - ExampleText
1 - 2 - ExampleText
3 - 1 - ExampleText
最佳答案
使用LINQ提供的GroupBy
扩展方法。
// Pseudo code
var grouped = list.GroupBy(item => item.UserID);
然后您可以调用 OrderByDescending
对结果进行排序:
var sorted = grouped.OrderByDescending(group => group.Count());
关于c# - 获取 List<T> 中最常见的项目并在之后排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21434903/