简体   繁体   English

如何在nopCommerce C#中将存储过程转换为linq

[英]How to convert store procedure to linq in nopCommerce c#

How to convert store procedure to linq in nopCommerce c# 如何在nopCommerce C#中将存储过程转换为linq

My store procedure query 我的存储过程查询

SELECT p.Id
FROM Product p WITH (NOLOCK)
LEFT JOIN Discount_AppliedToProducts dap WITH (NOLOCK)
    ON p.Id = dap.Product_Id
LEFT JOIN Product_Category_Mapping pcm WITH (NOLOCK)
    ON p.Id = pcm.ProductId
LEFT JOIN Discount_AppliedToCategories dac WITH (NOLOCK)
    ON pcm.CategoryId = dac.Category_Id
        AND dac.Category_Id IN (1 ,2 ,3 ,4 ,5 ,6)
LEFT JOIN Product_Manufacturer_Mapping pmm WITH (NOLOCK)
    ON p.Id = pmm.ProductId
LEFT JOIN Discount_AppliedToManufacturers dam WITH (NOLOCK)
    ON pmm.ManufacturerId = dam.Manufacturer_Id
WHERE dap.Discount_Id IN (3)
    OR dac.Discount_Id IN (3)
    OR dam.Discount_Id IN (3)

My linq query 我的linq查询

var productlist = (from q in _productRepository.Table
                                       select q).ToList();

var discount_AppliedToProductIds = (from dp in _discountRepository.Table
                                    from p in dp.AppliedToProducts
                                    select p).ToList().DistinctBy(d => d.Id).ToList();

var discount_AppliedToCategorieIds = (from dp in _discountRepository.Table
                                      from c in dp.AppliedToCategories
                                      select c).ToList().DistinctBy(d => d.Id).ToList();

var discount_AppliedToManufacturerIds = (from dp in _discountRepository.Table
                                         from m in dp.AppliedToManufacturers
                                         select m).ToList().DistinctBy(d => d.Id).ToList();

var product_Manufacturer_Mapping = (from dp in productlist
                                    from pm in dp.ProductManufacturers
                                    select pm).ToList().DistinctBy(d => d.Id).ToList();

var product_Category_Mapping = (from dp in productlist
                                from pc in dp.ProductCategories
                                select pc).ToList().DistinctBy(d => d.Id).ToList();

var ss = (from p in productlist
      join dap in discount_AppliedToProductIds on p.Id equals dap.Id
      join pcm in product_Category_Mapping on p.Id equals pcm.ProductId
      //join dac in discount_AppliedToCategorieIds on pcm.CategoryId equals dac.Id
      from dac in discount_AppliedToCategorieIds
      join pmm in product_Manufacturer_Mapping on p.Id equals pmm.ProductId
      join dam in discount_AppliedToManufacturerIds on pmm.ManufacturerId equals dam.Id
      from dapd in dap.AppliedDiscounts
      from pacd in dac.AppliedDiscounts
      from damd in dam.AppliedDiscounts
      where discountIds.Any(d => dapd.Id == d || d == pacd.Id || d == damd.Id)
      // innner join condition
      where categoryIds.Any(d => d == dac.Id) && dac.Id == pcm.CategoryId    
    select p).ToList();

I have write this code into c# but this code not provide proper result. 我已将此代码写入c#,但此代码未提供正确的结果。 Now I don't know what is problem into this code. 现在我不知道这段代码有什么问题。 If I run this code into sql server, then I get proper result, but in c# code I don't get proper result. 如果我将此代码运行到sql server中,那么我会得到正确的结果,但是在c#代码中我不会得到正确的结果。

It's been a long time since I wrote a query in LINQ, but I seem to recall that if you wish to model a LEFT JOIN, you have to use DefaultIfEmpty(), otherwise you end up with an INNER JOIN. 自从我在LINQ中编写查询以来已经有很长时间了,但是我似乎还记得,如果您希望对LEFT JOIN进行建模,则必须使用DefaultIfEmpty(),否则最终将得到一个INNER JOIN。

See this, it's answer shows where to apply DefaultIfEmpty: 看到这个,它的答案显示在哪里应用DefaultIfEmpty:

Linq join iquery, how to use defaultifempty Linq加入iquery,如何使用defaultifempty

Obviously if you don't correctly model a LEFT JOIN expression, you'll end up with results only when all 3 inputs produce values. 显然,如果您未正确建模LEFT JOIN表达式,则只有当所有3个输入都产生值时,您才能得到结果。

I would also suggest not using .ToList() on each of your source queries, because that's going to manifest data into memory and use LINQ to Objects for your final query. 我还建议不要在每个源查询中使用.ToList(),因为这会将数据清单到内存中,并使用LINQ to Objects作为最终查询。 If you remove the .ToList(), LINQ will construct a single database query for the entire process. 如果删除.ToList(),LINQ将为整个过程构造一个数据库查询。

Mark 标记

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM