An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
Use a set-based comparison instead of comparing timestamps.
For List<T>, LINQ provides set operators such as Except, Distinct, Union, and Intersect. For the specific requirement of finding records that are in the incoming list but not already present in the server-side data, Except is the relevant operator.
Two important points apply here:
- LINQ set operators compare elements by equality.
- For custom types, equality is based on
GetHashCodeandEquals, or on a custom comparer.
Since tblResults is a custom class and only Record_ID determines uniqueness, compare by Record_ID rather than by the full object instance.
Example with a custom comparer:
public class TblResultsRecordIdComparer : IEqualityComparer<tblResults>
{
public bool Equals(tblResults x, tblResults y)
{
if (ReferenceEquals(x, y)) return true;
if (x is null || y is null) return false;
return x.Record_ID == y.Record_ID;
}
public int GetHashCode(tblResults obj)
{
return obj.Record_ID.GetHashCode();
}
}
Then compare the incoming list with the existing records:
var comparer = new TblResultsRecordIdComparer();
var newRows = incomingResults.Except(existingResults, comparer).ToList();
newRows contains only items from incomingResults whose Record_ID does not already exist in existingResults.
If the data is coming from a database, the efficient pattern is still to do the comparison as a set operation and insert only the missing rows. Also ensure the database has an index or unique constraint on Record_ID, because that is what ultimately enforces uniqueness and improves lookup performance.
If only the IDs are needed for comparison, a simpler approach is to compare ID sequences directly:
var existingIds = existingResults.Select(r => r.Record_ID);
var newRows = incomingResults
.Where(r => !existingIds.Contains(r.Record_ID))
.ToList();
For large datasets, the set-based Except approach is the better LINQ fit from the available guidance, and indexing the join/comparison column is important for performance.
References: