相交两个List<>。在C#中 [英] Intersect two List<> in C#
本文介绍了相交两个List<>。在C#中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个要交叉的 List< T>
对象,但是尝试时遇到错误。
I have two List<T>
objects that I would like to intersect, but I get errors when trying.
// Make the Keys in the Dictionary<Load, double> _loads to form a List<Load>
List<Load> l1 = _loads.Keys.ToList();
// Get a list from my element.
List<Load> l2 = element.ListLoads;
// Intersect
List<Load> loads = (List<Load>)l1.Intersect<Load>(l2);
推荐答案
Intersect< T>
返回 IEnumerable< T>
,因此是正确的方法是:
Intersect<T>
returns an IEnumerable<T>
, so the correct way is:
var loads = l1.Intersect(l2).ToList();
ToList< T>
创建 List< T>
来自 IEnumerable< T>
。
请注意,调用 Intersect< T>
时可以省略type参数,编译器足够聪明,可以推断出
Note that you can omit the type argument when invoking Intersect<T>
, the compiler is smart enough to infer it.
这篇关于相交两个List<>。在C#中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文