收集“最近100个条目”行为

时间:2019-10-09 18:13:18

标签: c# collections

我想在C#中开发一种逻辑,在该逻辑中连续收集一些数据,一旦达到一定阈值(例如100),它将删除最旧的数据。换句话说,它仅显示最近的100个数据。 我尝试了类似这样的代码

average_grades = {}

for key, val in all_grades.items():
    x = statistics.mean(val)
    y = statistics.median(val)
    average_grades.update({key: [x, y]})
print(average_grades)

这会产生以下错误:

 class Test
    {
        public int Id { get; set; }
        public string Name { get; set; }

    }

class Program
{

    static void Main(string[] args)
    {
        int counter=0;
        List<Test> test = new List<Test>();

        // suppose this data is infinitely large

        for (int i = 0; i < 1000; i++)
        {
            test.Add(new Test { Id = i });
        }


        foreach (var item in test)
        {
            Console.WriteLine("Id={0},item.Id);
            counter++;
            // here I am trying to remove the first data so that the recent 
            // 100 data only stays in collection

            if (counter > 100)
            {
                test.RemoveAt(0);
            }
        } 

0 个答案:

没有答案