【问题标题】:Manipulation on all list items options for n lists in c#在c#中对n个列表的所有列表项选项进行操作
【发布时间】:2013-06-16 00:50:12
【问题描述】:

我想按属性值将我的数据拆分为列表,并检查列表项之间的所有组合选项。 我的问题是我不知道我会得到多少列表,以及除此之外是否有更好的方法:

var a = Data.Descendants("value").Where(x => x.Attribute("v").Value == "1").ToList(); var b = Data.Descendants("value").Where(x => x.Attribute("v").Value == "2").ToList(); var c = Data.Descendants("value").Where(x => x.Attribute("v").Value == "3").ToList();

foreach (var tempA in a) { foreach (var tempB in b) { foreach(c 中的 var tempC) { 做一点事; } } }

编辑: 我想从一个数据源 (var items = new List<string>{"1","1","2","3","2","1","3","3","2"}) 检查我的项目

现在我想将此列表拆分为 3 个列表 (list a = "1","1","1" - list b = "2","2","2" - list c = "3","3","3")

在这一步中,我要做的是检查从一个列表中的一项到其他列表中的其他项的所有组合。

a[0] with b[0] c[0]
a[0] with b[0] c[1]
a[0] with b[0] c[2]
a[0] with b[1] c[0]
.
.
b[1] with a[2] c[2]
.
.

谢谢!

【问题讨论】:

    标签: c# list foreach combinations


    【解决方案1】:

    您可以尝试使用 LINQ GroupBy 方法吗?这里有一些例子:

    LINQ GroupBy examples

    【讨论】:

    • 谢谢,这解决了第一个问题。你有第二个想法吗?如何遍历项目之间的所有组合
    • @Romoku 的 SelectMany/ForEach 建议能解决这个问题吗?
    【解决方案2】:

    您可以使用GroupBy 对元素进行分组。然后您可以使用 Linq 创建组合。

    var grouping = Data.Descendants("value")
                       .GroupBy(x => x.Attribute("v").Value);
    
    var combinations grouping.SelectMany(x =>
                                  grouping.Select(y =>
                                      new { Group = x, Combination = y }));
    
    foreach(var c in combinations)
    {
        //Do Something
    }
    

    例如

    public class Pair
    {
        public string A { get; set; }
        public string B { get; set; }
    }
    
    var pairs = new List<Pair>();
    pairs.Add(new Pair { A = "1", B = "2" });
    pairs.Add(new Pair { A = "1", B = "3" });
    pairs.Add(new Pair { A = "1", B = "4" });
    pairs.Add(new Pair { A = "2", B = "1" });
    pairs.Add(new Pair { A = "2", B = "2" });
    pairs.Add(new Pair { A = "2", B = "3" });
    
    var grouping = pairs.GroupBy(x => x.A);
    
    var combinations = grouping.SelectMany(x =>
                                    grouping.Select(y =>
                                        new { Group = x, Combination = y }));
    

    【讨论】:

      【解决方案3】:

      你可以这样做,遵循 romoku 和 chrisC 的思路

      //new list of lists to hold new information.
      List<List<Descendants>> NewList = new List<List<Descendants>>();
      
      foreach (var item in Data.Descendants.GroupBy(x => x.Attribute("v").Value))
      {
           NewList.Add(item.ToList());
      }
      

      对于您的新字符串编辑列表,这将做到这一点

      List<List<string>> NewList = new List<List<string>>();
      
      foreach (var item in OriginalList.GroupBy(x => x))
      {
            NewList.Add(item.ToList());
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-05-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-21
        • 2019-09-07
        • 2021-10-31
        相关资源
        最近更新 更多