繁体   English   中英

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

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

代码

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;
}

至于示例,我展示了“list1”“list2”“list3”,但我可能有超过 50 个列表为每个循环生成每个列表。 如何以编程方式将每个“列表”添加到IEnumerable 列表中以比较每个列表的数据?

我尝试了很多方法,例如转换为列表、添加、Append、Concat,但没有任何效果。

有没有其他最好的方法来比较 N 个列表?

output 代码:1 2

您可以创建列表列表并将列表动态添加到该列表中。 像这样的东西:

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);

要查找交叉点,您可以使用此解决方案,例如: Intersection of multiple lists with IEnumerable.Intersect() (实际上看起来就像您已经在使用的那样)。

还要确保更改GetCommonItems方法的签名:

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

您可以做的是允许GetCommonItems方法使用params关键字接受可变数量的参数。 这样,您就无需创建新的列表集合。

但是,不用说,如果源中的列表数量也是可变的,那么使用起来可能会比较棘手。

我还修改了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;
        }
    );
}

使用现有Main方法的替代解决方案。

编辑:根据此答案中的评论将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 是不可变的,因此您始终应根据需要返回 IEnumerable 的实现。
  2. 如果我理解正确,您想获得 N 个列表的常见项目。 我会为此使用 LINQ。

我的建议: 1. 制作一个包含所有项目的列表。 =>

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

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