In the C # list with DateTime element, delete all elements with a time 0 seconds before the latest measurement time.

3 minute read

background

list<class>There is a datetime type in the element of the type collection,
“Delete all elements with a time 0 seconds before the latest measurement time”
I wanted to remove it, so that memo.
I’m writing in WPF, so there are a few extra parts, but I love you.

Preparation

ExampleList.cs


class ExampleList
{
    public int Number { get; set; }
    public DateTime TimeStamp { get; set; }
}

Create a class to use for the list

MainWindow.cs


private List<ExampleList> _list = new List<ExampleList>();
private DateTime Dt; //Enter the latest time

public MainWindow()
{
    InitializeComponent();

    ListAdd();
    Console.WriteLine("Before deletion");
    ListCheck();
    Console.WriteLine("Dt(Latest time):" + Dt);
}

private void ListAdd()
{
    DateTime dt = DateTime.Now;
    for (int i = 1; i <= 10; i++)
    {
        DateTime dt2 = dt.AddSeconds(i); //dt +i seconds
        _list.Add(new ExampleList() { Number = i, TimeStamp = dt2 });
        Dt = dt2;
    }
}

private void ListCheck()
{
    foreach (ExampleList el in _list)
        Console.WriteLine("number:{0}Times of Day{1}", el.Number, el.TimeStamp);
}

listaddIn the method_Add an item to list.
listcheckIn the method_Check the contents of the list.


When executed

Before deletion
Number: 1 Time 2020/09/15 0:25:16
Number: 2 Time 2020/09/15 0:25:17
Number: 3 Time 2020/09/15 0:25:18
Number: 4 Time 2020/09/15 0:25:19
Number: 5 Time 2020/09/15 0:25:20
Number: 6 Time 2020/09/15 0:25:21
Number: 7 Time 2020/09/15 0:25:22
Number: 8 Time 2020/09/15 0:25:23
Number: 9 Time 2020/09/15 0:25:24
Number: 10 Time 2020/09/15 0:25:25
Dt(Latest time):2020/09/15 0:25:25

I made 10 list items like this.

Deletes elements before the specified number of seconds

This is the main subject.
Adds a method Remove to remove list elements that have a time earlier than the specified number of seconds.

MainWindow.cs


private void Remove()
{
    int s = -5;                                                 //Specifying the number of seconds
    DateTime ago = Dt.AddSeconds(s);                            //Time s seconds before the latest measurement
    int result = _list.FindLastIndex(t => t.TimeStamp <= ago);  //{Items in the list that have a value that is the specified number of seconds ago}Search for the highest index number
    _list.RemoveRange(0, result);                               //Delete list index 0-result
}



Execute from the MainWindow method

MainWindow.cs


public MainWindow()
{
    InitializeComponent();

    ListAdd();
    Console.WriteLine("Before deletion");
    ListCheck();
    Console.WriteLine("Dt(Latest time):" + Dt);
    Remove();
    Console.WriteLine("After deletion");
    ListCheck();
}



Execution result

Before deletion
Number: 1 Time 2020/09/15 0:25:16
Number: 2 Time 2020/09/15 0:25:17
Number: 3 Time 2020/09/15 0:25:18
Number: 4 Time 2020/09/15 0:25:19
Number: 5 Time 2020/09/15 0:25:20
Number: 6 Time 2020/09/15 0:25:21
Number: 7 Time 2020/09/15 0:25:22
Number: 8 Time 2020/09/15 0:25:23
Number: 9 Time 2020/09/15 0:25:24
Number: 10 Time 2020/09/15 0:25:25
Dt(Latest time):2020/09/15 0:25:25
After deletion
Number: 5 Time 2020/09/15 0:25:20
Number: 6 Time 2020/09/15 0:25:21
Number: 7 Time 2020/09/15 0:25:22
Number: 8 Time 2020/09/15 0:25:23
Number: 9 Time 2020/09/15 0:25:24
Number: 10 Time 2020/09/15 0:25:25

I was able to delete a list element that has a time earlier than the specified number of seconds.

The whole final code

class examplelistHas not changed from the beginning, so I will omit it.

MainWindow.cs


public partial class MainWindow : Window
{
    private List<ExampleList> _list = new List<ExampleList>();
    private DateTime Dt; //Enter the latest time

    public MainWindow()
    {
        InitializeComponent();
        ListAdd();
        Console.WriteLine("Before deletion");
        ListCheck();
        Console.WriteLine("Dt(Latest time):" + Dt);
        Remove();
        Console.WriteLine("After deletion");
        ListCheck();
    }

    private void ListAdd()
    {
        DateTime dt = DateTime.Now;
        for (int i = 1; i <= 10; i++)
        {
            DateTime dt2 = dt.AddSeconds(i); //dt +i seconds
            _list.Add(new ExampleList() { Number = i, TimeStamp = dt2 });
            Dt = dt2;
        }
    }

    private void Remove()
    {
        int s = -5;                                                 //Specifying the number of seconds
        DateTime ago = Dt.AddSeconds(s);                            //Time s seconds before the latest measurement
        int result = _list.FindLastIndex(t => t.TimeStamp <= ago);  //{Items in the list that have a value that is the specified number of seconds ago}Search for the highest index number
        _list.RemoveRange(0, result);                               //Delete list index 0-result
    }

    private void ListCheck()
    {
        foreach (ExampleList el in _list)
            Console.WriteLine("number:{0}Times of Day{1}", el.Number, el.TimeStamp);
    }
}