c# - 使用 linq 查找索引位置,但要考虑依赖于值的关系

标签 c# asp.net linq

什么是按值排序的最佳方法,找到项目的排名索引,但要考虑联系。第 5 名的索引可能有两个项目,因此跳过第 6 名,下一次迭代从第 7 名开始。执行此操作的最佳方法是进行分组并跟踪索引吗?

return teamTournamentResults
.OrderByDescending(t => t.RankingPoints)
.Select((item, index) => new { Item = item, Index = index })
.Select(q => new TeamSummaryResultsModel
                {
                                DivisionRanking = q.Index + 1,
                                RankingPoints= q.Item.RankingPoints,

最佳答案

请阅读我对问题的评论。

看例子:

List<int> mi = new List<int>(){1,2,2,5,6,7,7,7,8,9,10,10};

var qry = mi
        .Select(g=>new
            {
                number = g,
                rank = (from i in mi where i>g select i).Count()+1
            });

结果:

number rank
1      12 
2      10 
2      10 
5      9 
6      8 
7      5 
7      5 
7      5 
8      4 
9      3 
10     1 
10     1 

其他有用的资源:

Converting SQL Rank() to LINQ, or alternative

LINQ Query with grouping and ranking

Implementing RANK OVER SQL Clause in C# LINQ

关于c# - 使用 linq 查找索引位置,但要考虑依赖于值的关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30947403/

相关文章:

c# - 将 TextBox 绑定(bind)到 ObservableCollection.Count WPF?

c# - 使用互操作在 excel 中设置数字格式

.net - 如何保护我的 ASP.NET AJAX 应用程序?

c# - 非静态方法需要一个目标。找不到似乎有效的答案

c# - 匹配一个字段中的多个项目

c# - Composite Key EF Core 在使用 Fluent Api 时出错

javascript - 如果 CDN 不工作,如何加载本地文件

c# - 日期时间变量

c# - 在 LinQ 中使用 Group By 时出错

c# - 比较两个对象不调用 CompareTo 方法