Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql EFCore查询,按产品搜索词获取每个供应商的产品数量_Postgresql_Linq_E Commerce - Fatal编程技术网

Postgresql EFCore查询,按产品搜索词获取每个供应商的产品数量

Postgresql EFCore查询,按产品搜索词获取每个供应商的产品数量,postgresql,linq,e-commerce,Postgresql,Linq,E Commerce,我被困在一个价格比较网站的产品搜索查询中 共有3个表格: 产品: ProductId,Title 供应商: 本多利德 产品价格: ProductId、VendorId、Price ProductPrices是特定产品的所有供应商价格的映射表 现在我想能够搜索像羊毛衫蓝色的产品。这将获得所有销售包含单词毛衣蓝色的产品的供应商以及为每个供应商找到的产品数量 输出应为: {[ {VendorA,Found:23}, {VendorB,Found:2}, }} 到目前为止,我只有通过产品搜索词获取

我被困在一个价格比较网站的产品搜索查询中

共有3个表格:

产品: ProductId,Title

供应商: 本多利德

产品价格: ProductId、VendorId、Price

ProductPrices是特定产品的所有供应商价格的映射表

现在我想能够搜索像羊毛衫蓝色的产品。这将获得所有销售包含单词毛衣蓝色的产品的供应商以及为每个供应商找到的产品数量

输出应为:

{[

{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}");

过滤后,您需要在供应商上使用GroupBy,无需使用Distinct。感谢您的快速响应。运行此查询时,我收到以下错误:System.InvalidOperationException:不支持客户端GroupBy。请尝试分离代码,如var result1=来自产品中的产品在product.ProductId等于productPrice.ProductId,其中product.Title==选择新建{VendorId=productPrice.VendorId,tilt=product.Title}.ToList;Dictionary result2=result1.GroupByv=>v.VendorId.ToDictionaryk=>k.Key,v=>$Found:{v.ToList.Count};这正是我所需要的。非常感谢。我刚刚添加了一些额外的代码以获得完整的供应商对象作为响应:.ToDictionaryk=>k.Key,v=>new{a=$Found:{v.ToList.Count},b=v.Selectx=>x.Vendor.FirstOrDefault};