我正在尝试进行一个left outer join
也有一个查询custom comparator
.
我有以下列表:
List<ColumnInformation> list1;
List<ColumnInformation> list2;
Run Code Online (Sandbox Code Playgroud)
它们包含有关SQL列(数据类型,名称,表等)的信息.我已经Equals
为这个课程做了一番,并做了一个operator ==
和operator !=
.
我知道如何进行左外连接:
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
{
l1.ID,
ColumnName1 = l1.Name,
ColumnName2 = l2.Name,
};
Run Code Online (Sandbox Code Playgroud)
我了解如何制作和使用自定义IEqualityComparer
:
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},
cc);
Run Code Online (Sandbox Code Playgroud)
我的问题是:如何同时进行左外连接并同时使用我的比较器?
据我所知,查询语法没有比较器的关键字,扩展方法没有into
关键字的任何内容.
我不在乎结果是查询语法还是扩展方法.
您这样做的方法是使用GroupJoin
and SelectMany
(使结果变平):
ColumnComparer cc = new ColumnComparer();
var joinedList = list1
.GroupJoin(list2,
x => x,
y => y,
(x, g) => new {x, g},
cc)
.SelectMany(
z => z.g.DefaultIfEmpty(),
(z, b) => new { x = z.x, y = b }
);
Run Code Online (Sandbox Code Playgroud)