Linq to Entities 左外连接/子查询?
我有一个查询(在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该有助于 Linq-To-Entities 中的左外连接
此外,我想如果您想要
http:// msdn.microsoft.com/en-us/library/bb896272.aspx
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