我有一个列表列表,我想找到这样的交集:
var list1 = new List<int>() { 1, 2, 3 };
var list2 = new List<int>() { 2, 3, 4 };
var list3 = new List<int>() { 3, 4, 5 };
var listOfLists = new List<List<int>>() { list1, list2, list3 };
// expected intersection is List<int>() { 3 };
Run Code Online (Sandbox Code Playgroud)
有没有办法用IEnumerable.Intersect()做到这一点?
编辑:我应该更清楚这一点:我真的有一个列表列表,我不知道会有多少列表,上面的三个列表只是一个例子,我所拥有的实际上是一个 IEnumerable<IEnumerable<SomeClass>>
谢谢你的所有好评.事实证明,解决这个问题有四种选择:List +聚合(@Marcel Gosselin),List + foreach(@JaredPar,@ Gabe Moothart),HashSet +聚合(@jesperll)和HashSet + foreach(@Tony the Pony).我对这些解决方案进行了一些性能测试(不同数量的列表,每个列表中的元素数量和随机数最大值. …
我有三个不同类型的列表:
List<Customer> customerList = new List<Customer>();
List<Product> productList = new List<Product>();
List<Vehicle> vehicleList = new List<Vehicle>();
Run Code Online (Sandbox Code Playgroud)
我也有这个清单
List<string> stringList = {"AND","OR"};
Run Code Online (Sandbox Code Playgroud)
由于第一个元素stringList是AND我想用customerList和做内连接productList.然后我想vehicleList 与结果进行正确的连接,例如:
from cust in customerList
join prod in productList on cust.ProductId equals prod.Id
join veh in vehicleList on prod.VehicleId equals veh.Id into v
from veh in v.DefaultIfEmpty()
select new {customerName = cust.Name, customerVehicle=veh.VehicleName}
Run Code Online (Sandbox Code Playgroud)
我想以自动化的方式制作它,假设我有N多个列表和s和s的N-1数量,我该如何加入它们?此外,可以有许多相同类型的列表.这样的事情甚至可能吗?如果不是我能做些什么来使它更接近我的需要?提前致谢.ANDOR
编辑:我在这样的字典中持有列表及其类型:
var listDict = new Dictionary<Type, object>();
Run Code Online (Sandbox Code Playgroud)
所以如果有必要,我可以在这个字典里面迭代.