我被困在价格比较网站的产品搜索查询上。
有 3 张表:
产品:ProductId
,Title
供应商:VendorId
产品价格:ProductId
,VendorId
,Price
产品价格是特定产品的所有供应商价格的映射表。
现在我希望能够搜索像"毛衣蓝"这样的产品。这应该得到所有销售包含"毛衣蓝"一词的产品的供应商以及为每个供应商找到的产品数量。
输出应为:
{[
{VendorA,Found:23},
{VendorB,Found:2},
}}
到目前为止,我只有这个查询来通过产品搜索词获取所有供应商:
var query = Context.Products
.Join(Context.ProductPrices,
product => product.ProductId,
pprice => pprice.ProductId,
(product, pprice) => new { product, pprice })
.Join(Context.Vendors,
pprice2 => pprice2.pprice.VendorId,
vendor => vendor.VendorId,
(pprice2, vendor) => new { pprice2, vendor })
.Where(x=>x.pprice2.product.Title.Contains("sweater blue"))
.Distinct()
.Select(x=>new
{
x.vendor
});
我不知道如何在ProductPrices
中找到每个供应商的计数。
谢谢!
您可以只使用产品和产品价格表,如以下代码:
Dictionary<string, string> result = (from product in Context.Products
join productPrice in Context.ProductPrices on product.ProductId equals productPrice.ProductId
where product.Title == "sweater blue"
select new { VendorId = productPrice.VendorId, Tilte = product.Title }
)
.GroupBy(v => v.VendorId)
.ToDictionary(k => k.Key, v => $"Found : {v.ToList().Count}");