Radman
Radman

Reputation:

Linq to Entities Left Outer Join/ Subquery?

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.

Upvotes: 1

Views: 1590

Answers (1)

Maslow
Maslow

Reputation: 18746

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

Upvotes: 1

Related Questions