gunr2171 gunr2171 - 4 years ago 90
C# Question

Linq left outer join with custom comparator

I am trying to make a

left outer join
query that also has a
custom comparator

I have the following lists:

List<ColumnInformation> list1;
List<ColumnInformation> list2;

These hold information about an SQL column (datatype, name, table, etc). I have overrided
for the class, and made a
operator ==
operator !=

I understand how to make a left outer join:

var leftOuterJoin = from l1 in list1
join l2 in list2 on l1.objectID equals l2.objectID into temp
from l2 in temp.DefaultIfEmpty(new { l1.ID, Name = default(string) })
select new
ColumnName1 = l1.Name,
ColumnName2 = l2.Name,

And I understand how to make and use a custom

public class ColumnComparer : IEqualityComparer<ColumnInformation>
public bool Equals(ColumnInformation x, ColumnInformation y)
return x == y; //this uses my defined == operator

public int GetHashCode(ColumnInformation obj)
return 1; //forcing the join to use Equals, just trust me on this

ColumnComparer cc = new ColumnComparer();
var joinedList = list1.Join(list2,
x => x,
y => y,
(x, y) => new {x, y},

My question is: How can I do both a left outer join and use my comparator at the same time?

As far as I know, the query syntax does not have a keyword for a comparator, and the extension method does not have anything for the

I do not care if the result is in query syntax or extension methods.

Answer Source

The way you do that is with a GroupJoin and SelectMany (to flatten the results):

ColumnComparer cc = new ColumnComparer();
var joinedList = list1
        x => x,
        y => y,
        (x, g) => new {x, g},
        z => z.g.DefaultIfEmpty(),
        (z, b) => new  { x = z.x, y = b } 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download