我有两个类型为 IEnumerable
的实例,如下所示。
IEnumerable<Type1> type1 = ...;
IEnumerable<Type2> type2 = ...;
无论是 Type1
还是 Type2
都包含名为 common
的成员,因此即使它们属于不同的类,我们仍然可以像这样进行关联。
type1[0].common == type2[4].common
我正在尝试过滤掉那些在type2
中没有对应common
值的type1
元素,并且基于每个元素选择一个值创建一个字典。现在,我是通过下面的双重循环来实现这一点。
Dictionary<String, String> intersection = ...;
foreach (Type1 t1 in type1)
foreach(Type2 t2 in type2)
if (t1.common == t2.common)
intersection.Add(t1.Id, t2.Value);
现在,我已经尝试使用LINQ,但是所有的.Where
、.Select
和.ForEach
都让我头疼。有没有一种方法可以使用LINQ来整洁地执行相同的操作呢?