【问题标题】:Calculate all possible pairs of items from two lists?从两个列表中计算所有可能的项目对?
【发布时间】:2010-11-22 14:53:31
【问题描述】:

我有两个数组:

string[] Group = { "A", null, "B", null, "C", null };

string[] combination = { "C#", "Java", null, "C++", null }; 

我希望返回所有可能的组合,例如:

{ {"A","C#"} , {"A","Java"} , {"A","C++"},{"B","C#"},............ }

null 应该被忽略。

【问题讨论】:

    标签: c# .net linq combinations


    【解决方案1】:
    Group.Where(x => x != null)
         .SelectMany(g => combination.Where(c => c != null)
                                     .Select(c => new {Group = g, Combination = c})
         );
    

    或者:

    from g in Group where g != null
    from c in combination where c != null
    select new { Group = g, Combination = c }
    

    【讨论】:

    • 感谢 Mehrdad 的即时回复。
    • 任何想法如何有效地做到这一点来获得排列而不仅仅是组合? (意思是 {"A","C#"} 和 {"C#","A"} 将被视为两个单独的项目)?
    猜你喜欢
    • 1970-01-01
    • 2010-10-29
    • 1970-01-01
    • 1970-01-01
    • 2016-11-16
    • 2023-03-21
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    相关资源
    最近更新 更多