如何连接多个列表值并生成唯一名称

问题描述 投票:0回答:2

我有一个列表对象有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个独特的产品名称。

我怎样才能做到这一点?

c# asp.net-mvc
2个回答
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
投票

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

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:汽车[1:CarAttr1,2:CarAttr2]},{2:自行车[1:BikeAttr1,2:BikeAttr2]}]

© www.soinside.com 2019 - 2024. All rights reserved.