LINQ - retrieve differences between two generic Lists

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!
 
M

Martin Honnen

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));
 
M

Merk

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));
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top