You can use the sequence of GroupJoin
and SelectMany
methods. I made an example, in which DbSet
instances are presented with simple collection. Just to make it clear and reproducable:
[Test]
public void Linq_LeftOuterJoin()
{
// Arrange
List<A> A = new List<A>
{
new A { Id = 1},
new A { Id = 2},
new A { Id = 3},
new A { Id = 4}
};
List<B> B = new List<B>
{
new B { Id = 3},
new B { Id = 4}
};
// Act
var join = A.GroupJoin(B, a => a.Id, b => b.Id, (a, b) => new {a, b})
.SelectMany(t => t.b.DefaultIfEmpty(), (a, b) => new {a.a, b})
.Where(t => t.b == null)
.Select(t => t.a);
//Assert
Assert.AreEqual(join.Count(), 2);
Assert.AreEqual(join.Count(a => a.Id == 1), 1);
Assert.AreEqual(join.Count(a => a.Id == 2), 1);
}
Also please take a look on this question with many examples.
Hope it helps.