A表里数据如果连续5秒,就插入最后一秒的数据到另一张表;

public static List<TableData> FindConsecutiveDataWithinMinute(List<TableData> oldTable)
{
DateTime[] oldCreateTimeArray = oldTable.Select(t => t.CreateDatetime).ToArray();

int[] poorsArray = new int[oldCreateTimeArray.Length - 1];
for (int i = 1; i < oldCreateTimeArray.Length; i++)
{
TimeSpan timeDifference = oldCreateTimeArray[i] - oldCreateTimeArray[i - 1];
poorsArray[i - 1] = (int)timeDifference.TotalSeconds;
}

List<int> indexArray = new();
for (int i = 0; i <= poorsArray.Length - 4; i++)
{
if (poorsArray[i] == 1 && poorsArray[i + 1] == 1 && poorsArray[i + 2] == 1 && poorsArray[i + 3] == 1)
{
indexArray.Add(i + 3);
i += 3;
}
}

List<TableData> newTable = new();
foreach (int index in indexArray)
{
if (index >= 0 && index < oldTable.Count)
{
newTable.Add(oldTable[index + 1]);
}
}

return newTable;
}

posted @ 2024-02-27 18:26  爱吃糖的宝宝  阅读(1)  评论(0编辑  收藏  举报