简体   繁体   English

如何添加列表<t>项目动态到 IEnumerable<t></t></t>

[英]How to add List<T> items dynamically to IEnumerable<T>

Code代码

public static void Main()
{
    List<int> list1 = new List<int> {1, 2, 3, 4, 5, 6 };
    List<int> list2 = new List<int> {1, 2, 3 };
    List<int> list3 = new List<int> {1, 2 };
    var lists = new IEnumerable<int>[] { list1, list2, list3 };
    var commons = GetCommonItems(lists);
    Console.WriteLine("Common integers:");
    foreach (var c in commons)
        Console.WriteLine(c);
}

static IEnumerable<T> GetCommonItems<T>(IEnumerable<T>[] lists)
{
    HashSet<T> hs = new HashSet<T>(lists.First());
    for (int i = 1; i < lists.Length; i++)
        hs.IntersectWith(lists[i]);
    return hs;
}

As for the sample, I showed "list1" "list2" "list3", but I may have more than 50 lists that are generating each list using for each loop.至于示例,我展示了“list1”“list2”“list3”,但我可能有超过 50 个列表为每个循环生成每个列表。 How can I add programmatically each "list" to IEnumerable lists for comparing data of each list?如何以编程方式将每个“列表”添加到IEnumerable 列表中以比较每个列表的数据?

I tried many ways like conversion to list, Add, Append, Concat but nothing worked.我尝试了很多方法,例如转换为列表、添加、Append、Concat,但没有任何效果。

Is there any other best way to compare the N number of lists?有没有其他最好的方法来比较 N 个列表?

The output of Code: 1 2 output 代码:1 2

You can create a list of lists and add lists to that list dynamically.您可以创建列表列表并将列表动态添加到该列表中。 Something like this:像这样的东西:

var lists = new List<List<int>>();
lists.Add(new List<int> {1, 2, 3, 4, 5, 6 });
lists.Add(new List<int> {1, 2, 3 });
lists.Add(new List<int> {1, 2 });

foreach (var list in listSources)
    lists.Add(list);

var commons = GetCommonItems(lists);

To find intersections you can use this solution for example: Intersection of multiple lists with IEnumerable.Intersect() (actually looks like that's what you are using already).要查找交叉点,您可以使用此解决方案,例如: Intersection of multiple lists with IEnumerable.Intersect() (实际上看起来就像您已经在使用的那样)。

Also make sure to change the signature of the GetCommonItems method:还要确保更改GetCommonItems方法的签名:

static IEnumerable<T> GetCommonItems<T>(List<List<T>> lists)

What you could do is allow the GetCommonItems method to accept a variable amount of parameters using the params keyword.您可以做的是允许GetCommonItems方法使用params关键字接受可变数量的参数。 This way, you avoid needing to create a new collection of lists.这样,您就无需创建新的列表集合。

It goes without saying, however, that if the amount of lists in your source is variable as well, this could be trickier to use.但是,不用说,如果源中的列表数量也是可变的,那么使用起来可能会比较棘手。

I've also amended the GetCommonItems method to work like the code from https://stackoverflow.com/a/1676684/9945524我还修改了GetCommonItems方法,使其像https://stackoverflow.com/a/1676684/9945524中的代码一样工作

public static void Main()
{
    List<int> list1 = new List<int> { 1, 2, 3, 4, 5, 6 };
    List<int> list2 = new List<int> { 1, 2, 3 };
    List<int> list3 = new List<int> { 1, 2 };

    var commons = GetCommonItems(list1, list2, list3); // pass the lists here

    Console.WriteLine("Common integers:");
    foreach (var c in commons)
        Console.WriteLine(c);
}

static IEnumerable<T> GetCommonItems<T>(params List<T>[] lists)
{
    return lists.Skip(1).Aggregate(
        new HashSet<T>(lists.First()),
        (hs, lst) =>
        {
            hs.IntersectWith(lst);
            return hs;
        }
    );
}

Alternate solution using your existing Main method.使用现有Main方法的替代解决方案。

EDIT: changed the type of lists to List<List<int>> as per comment in this answer.编辑:根据此答案中的评论将lists类型更改为List<List<int>>

public static void Main()
{
    List<int> list1 = new List<int> { 1, 2, 3, 4, 5, 6 };
    List<int> list2 = new List<int> { 1, 2, 3 };
    List<int> list3 = new List<int> { 1, 2 };
    var lists = new List<List<int>> { list1, list2, list3 };
    var commons = GetCommonItems(lists);
    Console.WriteLine("Common integers:");
    foreach (var c in commons)
        Console.WriteLine(c);
}

static IEnumerable<T> GetCommonItems<T>(List<List<T>> lists)
{
    return lists.Skip(1).Aggregate(
        new HashSet<T>(lists.First()),
        (hs, lst) =>
        {
            hs.IntersectWith(lst);
            return hs;
        }
    );
}
  1. IEnumerable is immutable so you always should return an implementation of IEnumerable depending on your needs. IEnumerable 是不可变的,因此您始终应根据需要返回 IEnumerable 的实现。
  2. If I understand correctly you want to get common items of N lists.如果我理解正确,您想获得 N 个列表的常见项目。 I would use LINQ for this.我会为此使用 LINQ。

My proposition: 1. make one list that contains all of the items.我的建议: 1. 制作一个包含所有项目的列表。 => =>

var allElements = new List<int>();
  var lists = new List<List<int>>();
foreach (list in lists)
    allElements.AddRange(list);
  1. Take items that are repetitive采取重复的项目

allElements.GroupBy(x => x).Where(x => x.Count() > 1).Select(x => x).ToList();

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM