This may not satisfy you because the objects are not properly flattened, but you can perform an outer join and return a collection of anonymous types.
A left outer join is the same as the union of the inner join and the except set (the part of the first set which does not join onto the second set).
Here I simply
- calculate the inner join
- calculate the except set (creating an empty B entity to ensure the types are consistent)
- combine the two sets.
The LINQ is not beautiful, but it is cute.
class A
{
public int Id { get; set; }
public string PropertyA { get; set; }
}
class B
{
public int Id { get; set; }
public string PropertyB { get; set; }
}
var aThings = new List<A>();
var bThings = new List<B>();
var innerJoin = aThings.SelectMany(a =>
bThings.Where(b => a.Id == b.Id).Select(b => new { a, b })).ToList();
var exceptSet = aThings.Where(a =>
!bThings.Select(b => b.Id).Contains(a.Id)).Select( a =>
{
B b = new B();
return new { a, b };
});
var outerJoin = innerJoin;
outerJoin.AddRange(exceptSet);
The result is a List of anonymous types {a, b}