【问题标题】:Get the index of item in list based on value根据值获取列表中项目的索引
【发布时间】:2017-05-26 12:57:00
【问题描述】:

该场景适用于足球联赛表。我可以按比赛胜率和进球数对列表进行排序,以确定他们在联赛中的位置。然后,我使用此排序通过 IndexOf 函数获取球队在联赛表中的位置。

this.results = this.results.OrderByDescending(x => x.WinPercentage).ThenByDescending(x => x.Goals);


this.results.Foreach(x => x.Position = this.results.IndexOf(x));

当两支球队(应该是并列第一)有相同的比赛胜率和进球数时,问题就出现了,但是在获得索引时,一支球队将被分配到第一名,另一支球队被分配到第二名。

有没有办法获得正确的位置?

【问题讨论】:

  • 我猜你的问题没有解决方案,一旦两个项目(ANY)在一个列表中具有相同的分数,你将总是一个在另一个之后。您必须开发一种方法来假设两个相同分数的相同列表位置:我建议您创建一个二维数组或 LIST,其中一个值是数字位置,并放弃 LIST 位置作为联盟中每支球队的索引。
  • 是的,这就是我发布这个问题的原因,因为我目前的方法不起作用。我希望通过其他潜在的解决方案指出正确的方向。

标签: c# linq indexof


【解决方案1】:
 var position = 1;
 var last = result.First();
 foreach(var team in results)
 {
     if (team.WinPercentage != last.WinPercentage || team.Goals != last.Goals)
        ++position;

     team.Position = position;
     last = team;
 }

【讨论】:

  • 我认为该职位需要在 if 语句中分配给团队?
  • 你说得对。职位分配需要 if() 之后进行。我已经修好了。
【解决方案2】:

您可以做的是根据获胜百分比和目标对项目进行分组(如果两者相同,则团队将在同一组中),然后将相同的位置编号应用于同一组中的每个元素:

this.results = this.results.OrderByDescending(x => x.WinPercentage).ThenByDescending(x => x.Goals);

var positionGroups = this.results.GroupBy(x => new { WinPercentage = x.WinPercentage, Goals = x.Goals });
int position = 1;
foreach (var positionGroup in positionGroups)
{
    foreach (var team in positionGroup)
    {
        team.Position = position;
    }
    position++;
}

【讨论】:

  • 这是我正在考虑的方式(与 group by),但我想知道 James Currans 的回答是否更有效,因为它只包含一个 foreach 语句
  • @BadDub 如果您正在寻找,他的回答肯定会更有效。我只是想提供另一种选择。尽管GroupBy 不是解决此问题的最佳解决方案,但您现在可能会记住它的存在并将其列入您的保留曲目。
  • 感谢您的回复!
【解决方案3】:

下面的代码将为您工作

this.results = this.results.OrderByDescending(x => x.WinPercentage).ThenByDescending(x => x.Goals);


this.results.Foreach(x =>
{
    int index = this.results.FindIndex(y => y.Goals == x.Goals && y.WinPercentage == x.WinPercentage);
    x.Position = index > 0 ? this.results[index - 1].Position + 1 : 0;
});

【讨论】:

    【解决方案4】:

    这是我的解决方案

    1. 定义一个类:
    public class ABC
    {
        public int A { get; set; }
        public int B { get; set; }
        public int R { get; set; }
    }
    
    1. 构造数值:
    List<ABC> list = new List<ABC>();
    for (var i = 0; i < 100; i++)
    {
        list.Add(new ABC()
        {
            A = i,
            B = i > 50 && i < 70 ? i + 20 : i + 1
        });
    }
    
    1. 排名和打印值:
    var result = list.OrderByDescending(d => d.B)
                .GroupBy(d => d.B)
                .SelectMany((g, `i`) => g.Select(e => new ABC()
                {
                    A = e.A,
                    B = e.B,
                    R = i + 1
                })).ToList();
    
    foreach (var t in result)
    {
       Console.WriteLine(JsonConvert.SerializeObject(t));
    }
    Console.ReadLine();
    
    1. 结果:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-15
      • 2013-04-29
      • 2019-01-25
      • 1970-01-01
      • 1970-01-01
      • 2020-12-01
      • 1970-01-01
      • 2014-10-19
      相关资源
      最近更新 更多