Created
May 26, 2012 05:04
-
-
Save frankhale/2792321 to your computer and use it in GitHub Desktop.
LINQ join which returns differences
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
namespace ConsoleApplication4 | |
{ | |
class A | |
{ | |
public string Name { get; set; } | |
// other fields in here that are different than B | |
} | |
class B | |
{ | |
public string Name { get; set; } | |
// other fields in here that are different than A | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
// perhaps you could use the .Except() method but here is a join to find the | |
// differences between A and B | |
List<A> firstList = new List<A>() | |
{ | |
new A() { Name="Frank Hale" }, | |
new A() { Name="John Smith" }, | |
new A() { Name="Steve Jones" }, | |
}; | |
List<B> secondList = new List<B>() | |
{ | |
new B() { Name="Frank Hale" }, | |
}; | |
var query = from na in firstList | |
join nb in secondList | |
on na.Name equals nb.Name into joinedNames | |
from nb in joinedNames.DefaultIfEmpty() | |
where nb == null | |
select na; | |
foreach(A a in query) | |
{ | |
Console.WriteLine(a.Name); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment