1

我有一个查询(在 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}};

帮助.转储();

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

谢谢。

4

1 回答 1

1

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

此外,我想如果你愿意,你也可以向 EF 提交实际的 SQL

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

于 2010-02-11T18:50:36.013 回答