I'm looking for a better more efficient way to write some code I have where I"m synchronizing two lists. Basically, the first list is a list of devices I need to check. The second list is a list of devices that I have already checked with dates. I need to synchronize the two lists so I only check devices that are new or out of date based on the date. I also need to delete any devices that have been removed.
Here is my current code that works fine, it just looks and feels SO clunky.
public IEnumerable<AssetBlob> Synchronize(IEnumerable<AssetBlob> assets, string id) {
//Get list of devices already checked in lineitemsmap
List<AssetBlob> items = new List<AssetBlob>();
IEnumerable<LineItemsMap> lineItems = auditRepo.GetLineItemsMap(id).DeviceResults;
List<LineItemsMap> deleted = new List<LineItemsMap>();
//put new items into list
items.AddRange((from t0 in assets
join t1 in lineItems on t0.Id equals t1.BlobId into t1_join
from t1 in t1_join.DefaultIfEmpty()
where t1 == null
select t0).ToList());
//list of existing items that need updated
items.AddRange((from t0 in assets
join t1 in lineItems on t0.Id equals t1.BlobId
where t0.Imported > t1.Created
select t0).ToList());
deleted.AddRange((from t0 in assets
join t1 in lineItems on t0.Id equals t1.BlobId
where t0.Imported > t1.Created
select t1).ToList());
//Delete items in lineitems that don't exists in assets list
deleted.AddRange((from t0 in lineItems
join t1 in assets on t0.BlobId equals t1.Id into t1_join
from t1 in t1_join.DefaultIfEmpty()
where t1 == null
select t0).ToList());
if (deleted.Any()) {
auditRepo.RemoveLineItemMaps(deleted);
}
return items;
}
Any suggestion or improvements would be great. Thank you.