LINQ - retrieve differences between two generic Lists

  • Thread starter Thread starter Merk
  • Start date Start date
M

Merk

Using .NET 3.5 sp1, I have this class:

public class SiteComponentSynchInfo
{
public int SiteComponentID { get; set; }
public DateTime LastUpdatedDT { get; set; }
public string SiteComponentName { get; set; }
}

for which I store several instances, each in one of the following two Lists:

List<SiteComponentSynchInfo> sourceComponents = new
List<SiteComponentSynchInfo>();
List<SiteComponentSynchInfo> destinationComponents = new
List<SiteComponentSynchInfo>();

Given the above two Lists, I would like to have a LINQ query that returns a
List of all SiteComponentSynchInfo instances where the LastUpdatedDT is
newer in the sourceComponents than in the destinationComponents, for the
same SiteComponentID.

I separately need to know which SiteComponentSynchInfo instances exist in
the sourceComponents List but do not exist in the destinationComponents
List - basing this comparison on SiteComponentID (e.g., "which site
components - identified by SiteComponentID - are represented in
sourceComponents but not in destinationComponents?").

Any help here is greatly appreciated. I have been learning the basics of
LINQ and Lambdas, but I'm at a loss on this one.

Thanks!
 
Merk said:
Given the above two Lists, I would like to have a LINQ query that returns a
List of all SiteComponentSynchInfo instances where the LastUpdatedDT is
newer in the sourceComponents than in the destinationComponents, for the
same SiteComponentID.

What do you want to do for SiteComponentSynchInfo objects in
sourceComponents that do not have a matching object in
destinationComponents? If you want to ignore them then use

IEnumerable<SiteComponentSynchInfo> newerSource =
from info in sourceComponents
let dest = destinationComponents.FirstOrDefault(d =>
d.SiteComponentID == info.SiteComponentID)
where dest != null && info.LastUpdatedDT >
dest.LastUpdatedDT
select info;

I separately need to know which SiteComponentSynchInfo instances exist in
the sourceComponents List but do not exist in the destinationComponents
List - basing this comparison on SiteComponentID (e.g., "which site
components - identified by SiteComponentID - are represented in
sourceComponents but not in destinationComponents?").

IEnumerable<SiteComponentSynchInfo> sourceOnly =
sourceComponents.Where(s => !destinationComponents.Any(d =>
s.SiteComponentID == d.SiteComponentID));
 
Thank you. This works exactly as intended.

- M


Martin Honnen said:
What do you want to do for SiteComponentSynchInfo objects in
sourceComponents that do not have a matching object in
destinationComponents? If you want to ignore them then use

IEnumerable<SiteComponentSynchInfo> newerSource =
from info in sourceComponents
let dest = destinationComponents.FirstOrDefault(d =>
d.SiteComponentID == info.SiteComponentID)
where dest != null && info.LastUpdatedDT >
dest.LastUpdatedDT
select info;



IEnumerable<SiteComponentSynchInfo> sourceOnly =
sourceComponents.Where(s => !destinationComponents.Any(d =>
s.SiteComponentID == d.SiteComponentID));
 
Back
Top