【问题标题】:How to concatenate multiple list value and generate unique name如何连接多个列表值并生成唯一名称
【发布时间】:2019-04-25 10:41:32
【问题描述】:

我有一个列表对象,其中包含 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);
    }

我的输出是这样的:

现在我想要另一个与所有值名称连接的名称列表,例如:

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

我该怎么做?

【问题讨论】:

    标签: c# asp.net-mvc


    【解决方案1】:

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

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

    【讨论】:

    • 但是我的列表不是固定的,可以是3个或更高,这种情况我该怎么办???
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多