2 回答

TA貢獻1874條經(jīng)驗 獲得超12個贊
這就是你想要的嗎?迭代每個列表并組合所有可能性?
var first = new List<string> { "one", "two" };
var second = new List<string> { "middle" };
var third = new List<string> { "a", "b", "c", "d" };
var all = new List<List<string>> { first, second, third };
List<string> GetIds(List<List<string>> remaining)
{
if (remaining.Count() == 1) return remaining.First();
else
{
var current = remaining.First();
List<string> outputs = new List<string>();
List<string> ids = GetIds(remaining.Skip(1).ToList());
foreach (var cur in current)
foreach (var id in ids)
outputs.Add(cur + " - " + id);
return outputs;
}
}
var names = GetIds(all);
foreach (var name in names)
{
Console.WriteLine(name);
}
Console.Read();
結(jié)果如下:
one - middle - a
one - middle - b
one - middle - c
one - middle - d
two - middle - a
two - middle - b
two - middle - c
two - middle - d
從Generate all Combinations from Multiple (n) Lists復(fù)制并略微改編

TA貢獻1801條經(jīng)驗 獲得超8個贊
這是使用嵌套函數(shù)對對象進行字符串化的方法:
public static string GetUniqueName(IEnumerable<Attributes> source)
{
return "[{" + String.Join("},{", source.Select(AttributeToString)) + "}]";
string AttributeToString(Attributes a)
{
return a.AttributeId + ":" + a.AttributeName + "[" + String.Join(",",
a.AttributesValues.Select(ValueToString)) + "]";
}
string ValueToString(AttributesValue av)
{
return av.AttributeValueId + ":" + av.AttributeValueName;
}
}
使用示例:
var productAttributes = new string[] {"Car", "Bike"}.Select((s, i) => new Attributes()
{
AttributeId = i + 1,
AttributeName = s,
AttributesValues = new AttributesValue[]
{
new AttributesValue{AttributeValueId = 1, AttributeValueName = s + "Attr1"},
new AttributesValue{AttributeValueId = 2, AttributeValueName = s + "Attr2"},
}
});
Console.WriteLine(GetUniqueName(productAttributes));
輸出:
[{1:Car[1:CarAttr1,2:CarAttr2]},{2:Bike[1:BikeAttr1,2:BikeAttr2]}]
- 2 回答
- 0 關(guān)注
- 84 瀏覽
添加回答
舉報