简体   繁体   English

我在一个列表中有多个列表对象,如何获取每个子列表中存在的项目?

[英]I have multiple list objects in one list, how can i get the items that exist in every child list?

Starting with a basic class:从基本的 class 开始:

public class Car
{
  public string Name {get;set;}
}

I can then create a list of these cars然后我可以创建这些汽车的列表

List<Car> cars = new List<Car>();

The new step is to have a List of this List, like this:新的步骤是拥有此列表的列表,如下所示:

List<List<Car>> allListsOfCars = new List<List<Car>>();

After populating allListsOfCars, I want to pass it to a function which will return me the cars which exist in every List list.填充 allListsOfCars 后,我想将它传递给 function,它将返回每个列表列表中存在的汽车。

I know it sounds confusing, so I'll try explain a bit more.我知道这听起来很混乱,所以我会尝试多解释一下。

If I have ListA, ListB, ListC all of type List - and now combine these into 1 holding list(The list of a list), then how can I get back all the cars that exist in every list?如果我有 ListA、ListB、ListC 所有类型的 List - 现在将它们组合成 1 个保存列表(列表的列表),那么我怎样才能取回每个列表中存在的所有汽车? For example if a car only exists in ListA, then I'm not interested, it needs to exist in ListA AND ListB AND ListC, then I want it added to the result set and returned.例如,如果汽车只存在于 ListA 中,那么我不感兴趣,它需要存在于 ListA AND ListB AND ListC 中,然后我希望将它添加到结果集中并返回。

Thanks in advance.提前致谢。

You need to find the composite intersection of all the sublists.您需要找到所有子列表的复合交集。

IEnumerable<Car> result=allListsOfCars.FirstOrDefault();
if(result!=null)
{
    foreach(var sublist in allListsOfCars.Skip(1))
    {
        result=result.Intersect(sublist);
    }
    //enumerate result to run the query
}

It might be possible to rewrite using the Aggregate operator to eliminate the loop, but Aggregate never reads very well IMO.可能可以使用Aggregate运算符重写以消除循环,但Aggregate永远不会很好地读取 IMO。

If the lists are long, you'll probably get a decent speed increase using a HashSet如果列表很长,您可能会使用HashSet获得不错的速度提升

IEnumerable<Car> fst=allListsOfCars.FirstOrDefault();
if(result!=null)
{
    HashSet<Car> hs=new HashSet<Car>(fst);
    foreach(var sublist in allListsOfCars.Skip(1))
    {
        hs.IntersectWith(sublist); //in-place operation
    }
    //enumerate hs
}

Make sure that you Car class implements equality members and GetHashCode correctly, otherwise neither of these approaches will work as expected.确保您的Car class 正确实现了相等成员和GetHashCode ,否则这些方法都不会按预期工作。

If you have access to .NET 3.5 or later you can do the following:如果您有权访问 .NET 3.5 或更高版本,您可以执行以下操作:

IEnumerable<Car> carList = allListsOfCar.SelectMany(cars => cars);

EDIT:编辑:

To do the intersection of the lists you can do:要执行列表的交集,您可以执行以下操作:

List<Car> carList = allListsOfCar.Aggregate((left, right) => left.Intersect(right).ToList());

You can use Aggregate and I think this is a very real use case for it:您可以使用 Aggregate,我认为这是一个非常真实的用例:

var allCars = allListOfCars.Aggregate((listAcc,list2)=> listAcc.Concat(list2).ToList());

Bascially, for each element ( which in this case is a List<> ) concat it to the accumulator, getting a single list in the end.基本上,对于每个元素(在本例中为 List<> )将其连接到累加器,最后得到一个列表。

I tried the same approach as Adam, but expanded it a little.我尝试了与亚当相同的方法,但将其扩展了一点。

IEnumerable<Car> carList = listCars.SelectMany(cars => cars);

List<Car> repeatedCars = new List<Car>();

int length = listCars.Count;

foreach (Car c in cars1)
{
    int numberRepeats = carList.Count(car => car.Name == c.Name);
    if (numberRepeats == length)
    {
        repeatedCars.Add(c);
    }
}

Basically you need to know how many lists you have and save them all in a single list.基本上,您需要知道您有多少个列表并将它们全部保存在一个列表中。 Then just iterate through the first list of cars (or any of them) and count the number of cars with the same name in the list with all of the other lists.然后只需遍历第一个汽车列表(或其中任何一个)并计算列表中具有相同名称的汽车的数量以及所有其他列表。 If the length and the number of repeats is the same, then that car is in all of the lists.如果长度和重复次数相同,则该车在所有列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 lambda 的列表中获取一种类型的孩子? - How can I get one type of the child from a list by lambda? 如何在列表中找到多个项目&lt;&gt; - How can I find multiple items in a List<> 如何获取CollectionBase项目列表? - How can I get a list of CollectionBase items? 如何创建具有属性列表之一的行列表 - How can I create a list of rows that have one of a list of properties 我如何获得列表以及子列表,其中子列表在NHibernate中具有某些条件 - How can i get List along with child list where child list having some condition in NHibernate 如何在WPF Tookit PropertyGrid中列出子对象的属性? - How can i list properties of child objects in the WPF Tookit PropertyGrid? 如何比较两个对象列表的多个属性并返回一个新的匹配项目列表? - How can I compare multiple properties of two lists of objects and return a new list of matching items? 如何从项目列表中生成多个 forms? - how can I have multiple forms each generated from a list of items? 我需要将多个项目保存在 a.txt 文件中的 List&lt;&gt; 中,我该怎么做? 我没有任何代码可以显示 atm :') - I need to save multiple items in a List<> in a .txt file, how can I do that? I don't have any code to show atm :') 如何从列表中获取第 n 个项目<T> ? - How can I get every nth item from a List<T>?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM