使用LINQ查询排序

ulydmbyx  于 2022-12-06  发布在  其他
关注(0)|答案(3)|浏览(158)

给出了Good类型的商品goodList的信息序列和StorePrice类型的各个商店storePriceList中商品的价格序列。goodList序列的每个元素包括 * 产品SKU、类别、原产地 * 字段。storePriceList序列的每个元素包括 * 产品SKU、商店标题、价格 * 字段。
对于每个原产国,获取提供该国制造的商品的商店数量,以及所有商店中来自该国家的商品的最低价格(国家统计值)。如果在任何商店中均未找到某个国家的产品,则商店数量和最低价格将假定为0。按原产国对列表进行排序。
示例:

goodList: new[]
{
    new Good{Id = 1, Country = "Ukraine", Category = "Food"},
    new Good{Id = 2, Country = "Ukraine", Category = "Food"},
    new Good{Id = 3, Country = "Ukraine", Category = "Food"},
    new Good{Id = 4, Country = "Ukraine", Category = "Food"},
    new Good{Id = 5, Country = "Germany", Category = "Food"},
    new Good{Id = 6, Country = "Germany", Category = "Food"},
    new Good{Id = 7, Country = "Germany", Category = "Food"},
    new Good{Id = 8, Country = "Germany", Category = "Food"},
    new Good{Id = 9, Country = "Greece", Category = "Food"},
    new Good{Id = 10, Country = "Greece", Category = "Food"},
    new Good{Id = 11, Country = "Greece", Category = "Food"},
    new Good{Id = 12, Country = "Italy", Category = "Food"},
    new Good{Id = 13, Country = "Italy", Category = "Food"},
    new Good{Id = 14, Country = "Italy", Category = "Food"},
    new Good{Id = 15, Country = "Slovenia", Category = "Food"}
}

storePriceList: new[]
{
    new StorePrice{GoodId = 1, Price = 1.25M, Shop = "shop1"},
    new StorePrice{GoodId = 3, Price = 2.25M, Shop = "shop1"},
    new StorePrice{GoodId = 5, Price = 4.25M, Shop = "shop1"},
    new StorePrice{GoodId = 7, Price = 9.25M, Shop = "shop1"},
    new StorePrice{GoodId = 9, Price = 11.25M, Shop = "shop1"},
    new StorePrice{GoodId = 11, Price = 12.25M, Shop = "shop1"},
    new StorePrice{GoodId = 13, Price = 13.25M, Shop = "shop1"},
    new StorePrice{GoodId = 14, Price = 14.25M, Shop = "shop1"},
    new StorePrice{GoodId = 5, Price = 11.25M, Shop = "shop2"},
    new StorePrice{GoodId = 4, Price = 16.25M, Shop = "shop2"},
    new StorePrice{GoodId = 3, Price = 18.25M, Shop = "shop2"},
    new StorePrice{GoodId = 2, Price = 11.25M, Shop = "shop2"},
    new StorePrice{GoodId = 1, Price = 1.50M, Shop = "shop2"},
    new StorePrice{GoodId = 3, Price = 4.25M, Shop = "shop3"},
    new StorePrice{GoodId = 7, Price = 3.25M, Shop = "shop3"},
    new StorePrice{GoodId = 10, Price = 13.25M, Shop = "shop3"},
    new StorePrice{GoodId = 14, Price = 14.25M, Shop = "shop3"},
    new StorePrice{GoodId = 3, Price = 11.25M, Shop = "shop4"},
    new StorePrice{GoodId = 2, Price = 14.25M, Shop = "shop4"},
    new StorePrice{GoodId = 12, Price = 2.25M, Shop = "shop4"},
    new StorePrice{GoodId = 6, Price = 5.25M, Shop = "shop4"},
    new StorePrice{GoodId = 8, Price = 6.25M, Shop = "shop4"},
    new StorePrice{GoodId = 10, Price = 11.25M, Shop = "shop4"},
    new StorePrice{GoodId = 4, Price = 15.25M, Shop = "shop5"},
    new StorePrice{GoodId = 7, Price = 18.25M, Shop = "shop5"},
    new StorePrice{GoodId = 8, Price = 13.25M, Shop = "shop5"},
    new StorePrice{GoodId = 12, Price = 14.25M, Shop = "shop5"},
    new StorePrice{GoodId = 1, Price = 3.25M, Shop = "shop6"},
    new StorePrice{GoodId = 3, Price = 2.25M, Shop = "shop6"},
    new StorePrice{GoodId = 1, Price = 1.20M, Shop = "shop7"}
}

预期结果:

expected: new[]
{
    new CountryStat{Country = "Germany", MinPrice = 3.25M, StoresNumber = 5},
    new CountryStat{Country = "Greece", MinPrice = 11.25M, StoresNumber = 3},
    new CountryStat{Country = "Italy", MinPrice = 2.25M, StoresNumber = 4},
    new CountryStat{Country = "Slovenia", MinPrice = 0.0M, StoresNumber = 0},
    new CountryStat{Country = "Ukraine", MinPrice = 1.20M, StoresNumber = 7},
});

我有一个想法,将storedPriceListGoodId分组,然后选择min Price,但我不知道下一步该做什么。

disbfnqx

disbfnqx1#

  1. goodList通过将IdGoodId匹配来左联接storePriceList
    1.按Country分组
    1.请选择:
    3.1.获取Price的最小值
    3.2.删除具有非重复空值的Shop并执行计数
    1.按Country排序
(
    from a in goodList
    join b in storePriceList on a.Id equals b.GoodId into ab
    from b in ab.DefaultIfEmpty()
    group new 
    { 
        Country = a.Country, 
        Price = b == null ? 0 : b.Price,
        Shop = b == null ? null : b.Shop
    } by a.Country into g
    select new
    {
        Country = g.Key,
        MinPrice = g.Min(x => x.Price),
        StoresNumber = g.Where(x => x.Shop != null)
                        .Select(x => x.Shop)
                        .Distinct()
                        .Count()
    }
)
.OrderBy(x => x.Country)
.ToList();

Demo @ .NET Fiddle

l2osamch

l2osamch2#

var result = goodList
        .Select(x => x.Country).Distinct()
        .GroupJoin(
            goodList.Join(storePriceList, good => good.Id, price => price.GoodId,
                (good, goodsGroup) =>
                    new
                    {
                        Good = good,
                        Prices = goodsGroup
                    }), country => country, goods => goods.Good.Country,
            (country, goods) => new
            {
                Country = country,
                Goods = goods
            })
        .AsEnumerable()
        .Select(x =>
            new CountryStat
            {
                Country = x.Country,
                MinPrice = x.Goods.Any() ? x.Goods.Select(y => y.Prices).Min(y => y.Price) : decimal.Zero,
                StoresNumber = x.Goods.Any() ? x.Goods.Select(y => y.Prices).DistinctBy(y => y.Shop).Count() : 0
            })
        .OrderBy(x => x.Country)
        .ToList();
mlmc2os5

mlmc2os53#

你可以在一个列表中Select个国家,并删除重复的元素(因此是一个所有国家的列表)。从那里你可以很容易地把主列表分成每个国家的货物列表(List<(string Country, List<prices>)>

相关问题