I have the following list of Objects:
List<PacketData> packetList = new List<PacketData>();
public class PacketData{
public DateTime timestamp {get; set;}
public byte[] data {get; set;}
public bool isDuplicated {get; set;}
}
From that list I have to eliminate all data
duplicates in a time range based on timestamp
.
To remove the duplicates hide the following (it works, but I feel that the performance is not optimal):
//creo una lista para guardar el resultado
List<PacketData> newListPackets = new List<PacketData>();
//recorro la lista original
foreach (var packet in packetList)
{
//si el paquete no esta duplicado
if (!packet.isDuplicated )
{
//hago la busqueda utilizando LINQ
packetList.Where(
(p, k) =>
//paquetes que no esten duplicados
!p.isDuplicated &&
//donde la diferencia de tiempo sea menor a un segundo
Math.Abs((packet.timestamp - p.timestamp ).TotalSeconds) < 1 &&
//y que el paquete (arreglo de bytes) sean iguales
p.data.SequenceEqual(packet.data)
).Select(p =>
{
//el resultado, los marco como duplicados
p.isDuplicated = true;
return p;
}).ToList();
newListPackets.Add(packet);
}
}
I have used other methods but this is the most efficient so far. With a total of 46,633 items on the list, it gives me a processing time of 6 minutes with 16 seconds.
Thank you in advance for your time.