EFCore 查询以按产品搜索词获取每个供应商的产品数量
EFCore query to get amount of products for each vendor by product search term
我被困在价格比较网站的产品搜索查询中。
有 3 个表:
产品:
ProductId
、Title
供应商:
VendorId
产品价格:
ProductId
、VendorId
、Price
ProductPrices 是特定产品的所有供应商价格的映射table。
现在我希望能够搜索像 "sweater blue" 这样的产品。这应该得到所有销售包含单词 "sweater blue" 的产品的供应商以及为每个供应商找到的产品数量。
输出应该是:
{[
{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
中每个供应商的计数。
谢谢!
您可以只使用 Product 和 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}");
我被困在价格比较网站的产品搜索查询中。
有 3 个表:
产品:
ProductId
、Title
供应商:
VendorId
产品价格:
ProductId
、VendorId
、Price
ProductPrices 是特定产品的所有供应商价格的映射table。
现在我希望能够搜索像 "sweater blue" 这样的产品。这应该得到所有销售包含单词 "sweater blue" 的产品的供应商以及为每个供应商找到的产品数量。
输出应该是:
{[
{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
中每个供应商的计数。
谢谢!
您可以只使用 Product 和 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}");