我努力爲屬性列表及其可能的值生成所有可能的組合。我想實現是這樣的方法:爲屬性動態列表生成所有可能的組合及其值
public List<Variant> generateAllPossibleVariants(List<Attribute> attributes)
屬性類看起來如下:
public class Attribute {
public String Name { get; set; }
public ICollection<String> PossibleValues { get; protected set; }
}
所以,想象一下,你有2個屬性的列表(在計數是動態的)與其可能的值:
attributeColor with possible Values of ("red", "blue")
attributeSize with possible values of ("XL", "L")
現在我的方法應該返回變的名單,而variant類看起來如下:
現在public class Variant
{
public IDictionary<Attribute, string> AttributeValues { get; private set; }
}
我的方法應該返回類似下面的所有組合的列表:
List<Variant> :
Variant.AttributeValues { attributeColor => "red", attributeSize => "XL" }
Variant.AttributeValues { attributeColor => "red", attributeSize => "L" }
Variant.AttributeValues { attributeColor => "blue", attributeSize => "XL" }
Variant.AttributeValues { attributeColor => "blue", attributeSize => "L" }
這可能幫助:http://stackoverflow.com/questions/4073713/is-there-a-good-linq-way-do-a-cartesian-product – 2015-02-23 12:14:31
我真的認爲這就是爲什麼DB在哪裏做的,如果你把數據存儲在數據庫中它會更清晰如何存儲和拉動它。但這是我的看法。 – Liran 2015-02-23 12:29:27