c# - 获取按名称分组的列表的所有组合

标签 c# .net ienumerable generic-list

我有以下 TestParam 列表...这只是一个参数列表,用于确定查询将如何运行。在以下情况下,预期结果将针对不同参数的所有组合执行。因此,一个列表列表,其中 CustomerId 33 以及列表中可用的每个产品 ID...

List<TestParam> testList = new List<TestParam>();
        testList.Add(new TestParam() { Name = "CustomerId", Value = "33" });
        testList.Add(new TestParam() { Name = "ProductId", Value = "1" });
        testList.Add(new TestParam() { Name = "ProductId", Value = "2" });
        testList.Add(new TestParam() { Name = "ProductId", Value = "3" });
        testList.Add(new TestParam() { Name = "ProductId", Value = "4" });
        testList.Add(new TestParam() { Name = "ProductId", Value = "5" });
        testList.Add(new TestParam() { Name = "ProductId", Value = "6" });
        testList.Add(new TestParam() { Name = "ProductId", Value = "7" });
        testList.Add(new TestParam() { Name = "ProductId", Value = "8" });

TestParam 是一个普通的封装参数类,有名称和值...

public class TestParam
    {
        public string Name { get; set; }
        public string Value { get; set; }
    }

最终结果将是一个列表列表,其中 CustomerId 为 33,包含所有其他产品。如果我在 TestParam 列表中有不同的名称和值,也会得到相同的结果(以上只是一个例子)。

下面的代码,根据上面列表的组合以几个列表结束......

   // First get a list of distinct unique param collections...
    List<string> distinctParameterNames = new List<string>();
    testList.GroupBy(x => x.Name).ForEach(paramName => {
        distinctParameterNames.Add(paramName.Key);
    });

    // Get counts
    List<int> combinationList = new List<int>();
    foreach (var x in distinctParameterNames) { 
        combinationList.Add(testList.Where(y=>y.Name == x).Count());
    }

    // Will contain 2 lists, one having all combinations of parameters named CustomerId, and another with ProductId combinations...
    List<List<TestParam>> parameterList = new List<List<TestParam>>();
    foreach (var x in distinctParameterNames) {

        // Loop 
        List<TestParam> parameter = new List<TestParam>();
        testList.Where(paramName => paramName.Name == x).ForEach(y =>
        {

            parameter.Add(new TestParam() { Name = y.Name, Value = y.Value });

        });

        parameterList.Add(parameter);

    }

这将是列表之间的交集,最终结果将是一个列表列表,每个列表将具有以下组合......所以运行将返回(在这种情况下):

  1. 客户 33,产品 ID 1
  2. 客户 33,产品 ID 2
  3. 客户 33,产品 ID 3
  4. 客户 33,产品 ID 4
  5. 客户 33,产品 ID 5
  6. 客户 33,产品 ID 6
  7. 客户 33,产品 ID 7
  8. 客户 33,产品 ID 8

执行此操作最有效和通用的方法是什么?

最佳答案

以下是我一直在寻找的解决方案...

public static List<List<T>> AllCombinationsOf<T>(params List<T>[] sets)
        {
            // need array bounds checking etc for production
            var combinations = new List<List<T>>();

            // prime the data
            foreach (var value in sets[0])
                combinations.Add(new List<T> { value });

            foreach (var set in sets.Skip(1))
                combinations = AddExtraSet(combinations, set);

            return combinations;
        }

        private static List<List<T>> AddExtraSet<T>
     (List<List<T>> combinations, List<T> set)
        {
            var newCombinations = from value in set
                                  from combination in combinations
                                  select new List<T>(combination) { value };

            return newCombinations.ToList();
        }

用法(继续我的问题本身的代码片段):

var intersection = AllCombinationsOf(parameterList.ToArray());

关于c# - 获取按名称分组的列表的所有组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26993272/

相关文章:

c# - query.wait 没有完成

c# - 如何处理通过 Next 属性迭代的对象?

.net - Linq To Sql 多对多连接表

c# - 使用 Razor 按表中的日期对模型元素进行分组

c# - 使用 LINQ 检查 IEnumerable<T?> 的所有项目是否具有相同的值

c# - 每当有 UI 交互时,如何防止 CommandManager 调用 CanExecute?

c# - 具有额外属性的 Json.NET 序列化;保留序列化器设置

通过 C++ 互操作或其他方式进行 C# 一流延续?

c# - 泛型如何消除或减少装箱的需要?

c# - 为什么在枚举对象上设置属性不起作用?