代码之家  ›  专栏  ›  技术社区  ›  Shahar Shokrani

如何根据优先级顺序只选择一个选定的元素及其同级元素

  •  0
  • Shahar Shokrani  · 技术社区  · 4 年前

    问题描述:

    Manufacturers 每个包含一个 ManufacturerId Products ,其中包含 ProductId 以及嵌套的列表 Qualities QualityId 还有一个 Price .

    --Manufacturers (List) 
    ----ManufacturerId (string)
    ----Products (List)
    ------ProductId (string)
    ------Qualities (List) 
        -QualityId (string)
        -Price (decimal)
    

    或者

    class Manufacturer { string ManufacturerId { get; set; } IEnumerable<Product> Products { get; set; } }
    class Product { string ProductId { get; set; } IEnumerable<Quality> Qualities { get; set; } }
    class Quality { string QualityId { get; set; } decimal Price { get; set; } }
    

    我的目标:

    List<string> 其中每个项都是由 $"{ManufacturerId}-{ProductId}-{QualityId}" 钥匙。

    如何选择制造商和产品:

    1. string[] ProductPriotizeList => new[] { "P1", "P8", "P5", "P2" ... };
    2. 以最便宜的价格订购 质量ID .
    3. 构造 钥匙 被选中的 质量ID

    我创建了一个名为 FlattenQuality 包含 Quality 属性并添加了 制造商ID 以及

    class FlattenQuality 
    { 
        string QualityId { get; set; }
        string ManufacturerId { get; set; }
        string ProductId { get; set; }
        decimal Price { get; set; }
        string Key { get { return $"{ManufacturerId}-{ProductId}-{QualityId}"; }
    }
    

    然后构建了一个 扁平化质量 通过 SelectMany 超过制造商和 在产品上:

    List<FlattenQuality> flattenQualities = BuildFlattenQuality(..);
    

    List<string> selected = flattenQualities.OrderBy(x => Array.IndexOf(ProductPriotizeList, x.ProductId)).ThenBy(x => x.Price).Select(x => x.MyKey).ToList();
    

    例如:

    {
        "Manufacturers": [
        {
            "ManufacturerId": 1,
            "Products": [
            {
                "ProductId": P1,
                "Qualities": [
                {
                    "QualityId": 1,
                    "Price": 10
                },
                {
                    "QualityId": 2,
                    "Price": 20
                }]
            }]
        },
        {
            "ManufacturerId": 2,
            "Products": [
            {
                "ProductId": P1,
                "Qualities": [
                {
                    "QualityId": 1,
                    "Price": 15
                },
                {
                    "QualityId": 2,
                    "Price": 30
                }]
            }]
        }]
    }
    

    {
        "Keys": [
            "1-1-1", //The QualityId 1 of productId 1 of ManuId 1 is the cheapest
            "2-1-1"  //The cheapest sibling
        ]
    }
    

    我的问题:

    如何在不污染我的应用程序的情况下获得同样的结果?不用再重复所有的列表。

    0 回复  |  直到 4 年前
        1
  •  2
  •   OfirD    4 年前

    你要做的是把你的数据展平。为此:

    var records = 
        manufacturers.SelectMany(m => 
            m.Products.SelectMany(p =>
                p.Qualities.Select(q => new 
                { 
                    ManufacturerId = m.ManufacturerId,
                    ProductId = p.ProductId, 
                    QualityId = q.QualityId, 
                    Price = q.Price,
                    Key = string.Join("-", new string [] {m.ManufacturerId, p.ProductId, q.QualityId })
                })
            )
        );
    

    然后应用您的订购逻辑:

    records.OrderBy(x => Array.IndexOf(productPriotizeList, x.ProductId))
           .ThenBy(x => x.Price);
    

    here .