MD MASUM
MD MASUM

Reputation: 49

How to concatenate multiple list value and generate unique name

I have a list object with 3-4 item like (color, size, unit) and every list has another list like color list have multiple value (red, green, blue). I want to generate names like (red-xl-pcs)(red-xxl-pcs)(blue-xl-pcs)(blue-xxl-pcs)

My Model:

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

My controller:

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

My output is like this: enter image description here

Now I want another list of names concatenated with all value names like:

(12/y - cotton - green), (12/y - cotton - yellow) .... it will generate 8 unique product names.

How can I do this?

Upvotes: 0

Views: 441

Answers (2)

Theodor Zoulias
Theodor Zoulias

Reputation: 43911

Here is method that uses nested functions to stringify the objects:

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

Usage example:

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

Output:

[{1:Car[1:CarAttr1,2:CarAttr2]},{2:Bike[1:BikeAttr1,2:BikeAttr2]}]

Upvotes: 1

Sam
Sam

Reputation: 1707

Is this what you're after? Iterating over each list and combining all possibilities?

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

results in the following:

one - middle - a
one - middle - b
one - middle - c
one - middle - d
two - middle - a
two - middle - b
two - middle - c
two - middle - d

copied and slightly adapted from Generate all Combinations from Multiple (n) Lists

Upvotes: 2

Related Questions