第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何連接多個列表值并生成唯一名稱

如何連接多個列表值并生成唯一名稱

C#
翻翻過去那場雪 2022-12-31 10:50:42
我有一個列表對象,其中包含 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);    }我的輸出是這樣的: 現(xiàn)在我想要另一個與所有值名稱連接的名稱列表,例如:(12/y - cotton - green), (12/y - cotton - yellow) .... 它將生成 8 個唯一的產(chǎn)品名稱。我怎樣才能做到這一點?
查看完整描述

2 回答

?
HUWWW

TA貢獻1874條經(jīng)驗 獲得超12個贊

這就是你想要的嗎?迭代每個列表并組合所有可能性?


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

結(jié)果如下:


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復(fù)制并略微改編



查看完整回答
反對 回復(fù) 2022-12-31
?
蝴蝶刀刀

TA貢獻1801條經(jīng)驗 獲得超8個贊

這是使用嵌套函數(shù)對對象進行字符串化的方法:


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


查看完整回答
反對 回復(fù) 2022-12-31
  • 2 回答
  • 0 關(guān)注
  • 84 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號