简体   繁体   English

将sql查询重写为LINQ。 找不到错误

[英]Rewrite sql query to LINQ. Can't find an error

This is an SQL query: 这是一个SQL查询:

SELECT Website,VendorID,Name,LinkProduct,
            Link,Logo,Image,NameExtra as Industry,
            (SELECT [Percent] FROM Web_Promotion 
            WHERE Web_Promotion.VendorID=Web_Vendor.VendorID) 
            AS PercentOff  
            FROM Web_Vendor WHERE Active='1' AND 
            (VendorID IN (Select VendorID FROM Web_Promotion 
            WHERE VendorID<>'' AND Static='True' AND [Percent] <> '0' AND 
            ((Expires>=GETDATE()) OR (Expires IS NULL))) OR 
            VendorID IN (SELECT TOP 1 SC1 FROM NavItems 
            WHERE SC1=Web_Vendor.VendorID AND Promotion<>'' 
            AND ((PromotionStart<=GETDATE() AND PromotionEnd>=GETDATE()) 
            OR (PromotionStart<=GETDATE() AND PromotionEnd IS NULL))))
            ORDER BY NameExtra,Sequence 

I need to rewrite it to LINQ. 我需要将其重写为LINQ。 So this is my LINQ: 这是我的LINQ:

return await _db.Web_Vendor.
                    Where(x => !(x.WebPromotion.VendorID == string.Empty || x.WebPromotion.VendorID == null)
                    && x.WebPromotion.Static == true && x.WebPromotion.Percent != 0 &&
                    (x.WebPromotion.Expires >= DateTime.Now || x.WebPromotion.Expires == null)
                    ||
                    (_db.NavItems.Where(y => x.WebPromotion.VendorID == y.SC1
                        && !(y.Promotion == "" || y.Promotion == null)
                        && (y.PromotionStart <= DateTime.Now) && (y.PromotionEnd >= DateTime.Now || y.PromotionEnd == null))
                        .Select(g => g.SC1).Take(1).Contains(x.WebPromotion.VendorID)))
                    .Include(x => x.WebPromotion).Where(x => x.Active == true).OrderBy(x => x.NameExtra)
                    .ThenBy(x => x.Sequence).ToListAsync();

I spent about three ours, but can't find an error. 我花了我们三个人的钱,但找不到错误。 Original SQL query returns 16 rows, but my LINQ code returns only 13 of the. 原始SQL查询返回16行,但是我的LINQ代码仅返回13行。 Unfortunately I have only one navigation property (Web_Vendor <-> Web_Promotion). 不幸的是,我只有一个导航属性(Web_Vendor <-> Web_Promotion)。 I think that an error in the second part of my query: 我认为查询第二部分出现错误:

||
                        (_db.NavItems.Where(y => x.WebPromotion.VendorID == y.SC1
                            && !(y.Promotion == "" || y.Promotion == null)
                            && (y.PromotionStart <= DateTime.Now) && (y.PromotionEnd >= DateTime.Now || y.PromotionEnd == null))
                            .Select(g => g.SC1).Take(1).Contains(x.WebPromotion.VendorID)))

Can any expert check my code and help me? 任何专家都可以检查我的代码并为我提供帮助吗? Correct data: http://prntscr.com/9a5xwu Linq data (not correct) contains the same data as correct instead of values where PercentOff is null. 正确的数据: http : //prntscr.com/9a5xwu Linq数据(不正确)包含与正确的数据相同的数据,而不是PercentOff为null的值。 The main problem is that LINQ generate inner join instead of left join in this place: http://prntscr.com/9a6stb 主要问题是LINQ在此位置生成内部联接而不是左联接: http : //prntscr.com/9a6stb

since you say that your linq data miss the case when PercentOff = null, i'd focus on that 因为您说过您的linq数据缺少PercentOff = null的情况,所以我将重点放在

I guess your "PercentOff" in Linq is Percent property, and i see that you have in your where: "x.WebPromotion.Percent != 0" 我猜您在Linq中的“ PercentOff”是Percent属性,并且我看到您在您的位置:“ x.WebPromotion.Percent!= 0”

Is that a nullable value or you convert the null to the default property type, that is 0? 那是可为空的值,还是您将null转换为默认属性类型,即0?

couldn't be that null is converted to 0 and then the query skip it? 难道不是将null转换为0,然后查询将其跳过吗?

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

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