Skip to content

Instantly share code, notes, and snippets.

@frankhale
Created May 26, 2012 05:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frankhale/2792321 to your computer and use it in GitHub Desktop.
Save frankhale/2792321 to your computer and use it in GitHub Desktop.
LINQ join which returns differences
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