【问题标题】:Remove oldest n Items from List using C#使用 C# 从列表中删除最旧的 n 项
【发布时间】:2018-04-28 01:47:36
【问题描述】:

我正在制作一个经常更新的分数动态列表。最终,这用于产生整体评级,因此需要删除较旧的条目(基于某些参数,而不是时间),以防止对整体的 +/- 权重过重。它将从一个单独的枚举中一次添加多个值。

  List<int> scoreList = new List<int>();

  foreach(Item x in Items)
  { 
     scoreList.Add(x.score);
  }

  //what I need help with:
  if(scoreList.Count() > (Items.Count() * 3))
  {
      //I need to remove the last set (first in, first out) of values size 
      //Items.Count() from the list
  }

如果有人能提供帮助,将不胜感激 :) 我不得不让代码有点通用,因为它写得相当神秘(没有写方法)。

【问题讨论】:

  • 如果你需要 FIFO,你可以使用 Queue<int> 而不是 List
  • 对未来读者的警告:标题最初说的是“.. 最后 n 个项目”,但接受的答案删除了 first n 个项目,因为它们是最旧的,基于有问题的代码示例。下面的一些答案确实删除了“Last n”,因此请仔细阅读答案,看看它们是否符合您的要求。
  • 如果要对数据进行排序,请不要使用基于队列的实现

标签: c#


【解决方案1】:

使用List&lt;T&gt;.RemoveRange - 类似这样:

// number to remove is the difference between the current length
// and the maximum length you want to allow.
var count = scoreList.Count - (Items.Count() * 3);
if (count > 0) {
    // remove that number of items from the start of the list
    scoreList.RemoveRange(0, count);
}

您从列表的开头删除,因为当您 Add 时,它们会移到末尾 - 所以最旧的位于开头。

【讨论】:

    【解决方案2】:

    试试这个

    scoreList.RemoveAt(scoreList.Count-1);
    

    here 是 MSDN 文章

    【讨论】:

    • 删除最近添加的项目。 OP 想要删除最旧的项目
    • 在我的情况下,我只需要删除最后一项,所以这对我来说非常有效。谢谢。
    • RemoveAt 对于删除 one 项很有用。如果删除 N 个项目,RemoveRange 更合适。 Blorgbeard 的回答显示删除 first N 个项目;要删除 last N 项,请执行 scoreList.RemoveRange(scoreList.Count - N, N);。如果列表可能包含少于N 项,首先确保有N 项:if (scoreList.Count &gt;= N) ...; 或执行scoreList.RemoveRange(Math.Max(0, scoreList.Count - N), N);
    【解决方案3】:

    我建议不要使用List&lt;int&gt;,而是使用Queue&lt;int&gt;。这将为您提供您正在寻找的 FIFO 行为。

    有关队列的更多信息,请参阅http://msdn.microsoft.com/en-us/library/7977ey2c.aspx

      Queue<int> scoreList = new Queue<int>();
    
      foreach(Item x in Items)
      { 
         scoreList.Enqueue(x.score);
      }
    
      //Or you can eliminate the foreach by doing the following
      //Queue<int> scoreList = new Queue<int>(Items.Select(i => i.score).ToList());
    
      //Note that Count is a property for a Queue
      while (scoreList.Count > (Items.Count() * 3))
      {
         scoreList.Dequeue();
      }
    

    【讨论】:

      【解决方案4】:

      我不太明白你的问题,希望这是你想要的。

      scoreList.RemoveRange(Items.Count()*3, scoreList.Count()-Items.Count()*3);
      

      【讨论】:

        【解决方案5】:

        使用 linq 从列表中获取最后 n 个元素的简单方法

              scoreList.Skip(Math.Max(0, scoreList.Count() - N)).Take(N)
        

        【讨论】:

          【解决方案6】:

          我玩弄并查看了上面建议的方法(scoresList.RemoveAt()),但它不适合这种情况。什么最终起作用了:

           if (...)
           {
              scoresList.RemoveRange(0, scores.Count);
           }
          

          感谢大家的帮助

          【讨论】:

          • 这似乎会删除最近添加的分数,而不是最近添加的分数?
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-10-26
          • 1970-01-01
          • 2022-06-24
          • 2011-12-24
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多