Linq to Entities 左外连接/子查询?

发布于 2024-08-01 14:10:24 字数 1274 浏览 6 评论 0原文

我有一个查询(在 linqpad 中开发):

DateTime currentDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
DateTime previousMonth = currentDate.AddMonths(-1);
DateTime previousMonthForAveragePrices = currentDate.AddMonths(-2);


var help =  from cd in CostDrivers.OfType<Commodity>() 
                                  where cd.isActive == true && 
                                  cd.arePricesCalculatedAverage == false
                                  from cp in cd.CostDriverPrices where cp.priceDate
== currentDate 
                                  select new {cd.costDriverID, cd.costDriverName, cd.isUpdatedMonthly, cd.arePricesCalculatedAverage,
                                                cp.costDriverPriceID, priceDate = cp.priceDate,
                                                cp.price, previousPriceDate = from cpc in cd.CostDriverPrices where cpc.priceDate == previousMonth 
                                                select new {previousPrice = cpc.price, previousPriceDate = cpc.priceDate}};

help.Dump();

我需要做的是返回所有 costDrivers,无论给定日期(currentDate)是否存在价格记录。 我应该指出,尝试使用子查询来获取 currentDate -1 个月的另一个价格记录。 我试过了|| null 等不行。 这是实体的链接。 查询本身有效......它只会返回有价格的结果。 谢谢!

谢谢。

I have a query (developing in linqpad):

DateTime currentDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
DateTime previousMonth = currentDate.AddMonths(-1);
DateTime previousMonthForAveragePrices = currentDate.AddMonths(-2);


var help =  from cd in CostDrivers.OfType<Commodity>() 
                                  where cd.isActive == true && 
                                  cd.arePricesCalculatedAverage == false
                                  from cp in cd.CostDriverPrices where cp.priceDate
== currentDate 
                                  select new {cd.costDriverID, cd.costDriverName, cd.isUpdatedMonthly, cd.arePricesCalculatedAverage,
                                                cp.costDriverPriceID, priceDate = cp.priceDate,
                                                cp.price, previousPriceDate = from cpc in cd.CostDriverPrices where cpc.priceDate == previousMonth 
                                                select new {previousPrice = cpc.price, previousPriceDate = cpc.priceDate}};

help.Dump();

What i need to do is return ALL costDrivers regardless of whether a price record exists on the given date (currentDate). I should point out there is an attempt at a subquery to grab another price record for a the currentDate -1 month. I've tried || null etc. no go. This is linq to entities. The query itself works.. it will only return result where there is a price. thanks!

thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

始终不够 2024-08-08 14:10:24

This should help Left Outer Join in Linq-To-Entities

Additionally, I imagine you could also submit actual SQL to the EF if you wanted

http://msdn.microsoft.com/en-us/library/bb896272.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文