【发布时间】:2014-01-10 23:27:57
【问题描述】:
我正在寻找一种方法来比较两个列表中的对象。列表中的对象有两种不同的类型,但共享一个键值。例如
public class A
{
public string PropA1 {get;set;}
public string PropA2 {get;set;}
public string Key {get;set;}
}
public class B
{
public string PropB1 {get;set;}
public string PropB2 {get;set;}
public string Key {get;set;}
}
var listA = new List<A>(...);
var listB = new List<B>(...);
获取类型 A 的对象列表(其中键不存在于 listB 中)、类型 B 的对象列表(其中键不存在于 listA 中)以及连接列表的最快方法是什么具有匹配键的对象?我已经设法使用 Linq 创建了加入列表:
var joinedList = listA.Join(listB,
outerkey => outerkey.Key,
innerkey => innerkey.Key,
(a, b) => new C
{
A = a,
B = b
}).ToList();
但这当然只包含匹配的对象。有没有办法获取其他列表?
【问题讨论】: