如何串联多个列表值并生成唯一名称

时间:2019-04-25 10:41:32

标签: c# asp.net-mvc

我有一个包含3-4个项的列表对象,例如(颜色,大小,单位),每个列表都有另一个列表,例如颜色列表具有多个值(红色,绿色,蓝色)。我想生成类似(red-xl-pcs)(red-xxl-pcs)(blue-xl-pcs)(blue-xxl-pcs)

我的模特:

public class Attributes
{
    public int AttributeId { get; set; }
    public string AttributeName { get; set; }

    public IEnumerable<AttributesValue> AttributesValues { get; set; }
}

public class AttributesValue
{
    public int AttributeValueId { get; set; }
    public string AttributeValueName { get; set; }
}

我的控制器:

public async Task<ActionResult> GetProductAttribute(int productId)
    {
        LoadSession();
        var productAttributes = await _objProductDal.GetProductAttribute(productId, _strWareHouseId, _strShopId);


        foreach (var attribute in productAttributes)
        { 
            attribute.AttributesValues = await _objProductDal.GetProductAttributeValue(productId, attribute.AttributeId, _strWareHouseId, _strShopId);
        }

        return PartialView("_AttributeTablePartial", productAttributes);
    }

我的输出是这样的: enter image description here

现在我想要另一个与所有值名称串联在一起的名称列表:

(12 / y-棉-绿色),(12 / y-棉-黄色)....它将生成8个唯一的产品名称。

我该怎么做?

2 个答案:

答案 0 :(得分:2)

这是您的追求吗?遍历每个列表并结合所有可能性?

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();

导致以下结果:

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复制并稍作改编

答案 1 :(得分:1)

这是使用嵌套函数对对象进行字符串化的方法:

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]}]