Linq to Entities Left Outer Join / Subquery?

时间:2009-08-12 17:28:20

标签: linq-to-entities left-join

我有一个查询(在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)是否存在价格记录。我应该指出,有一个子查询尝试获取当前日期-1个月的另一个价格记录。我试过了|| null等没有去。这是实体的linq。查询本身有效..它只返回有价格的结果。谢谢!

感谢。

1 个答案:

答案 0 :(得分:1)

这应该有助于Left Outer Join in Linq-To-Entities

此外,我想如果你想要

,你也可以向EF提交实际的SQL

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