gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:45:52 24 4
gpt4 key购买 nike

我有以下 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com