I have a query that looks like this ....
var q = Dal.TBLINVENTORies.Where(i => i.SHOWIT); q = q.Where(i => i.dtStart < DateTime.Now || i.dtStart == null); q = q.Where(i => i.dtEnd > DateTime.Now || i.dtEnd == null); q = q.Where(i => i.sSystem.Contains("OE")); q = q.Where(i => i.WS_ActiveList_ID == 0 || i.tblWS_ActiveList.WS_MasterList_ID == 16); var test2 = q.ToList();
Just before "ToList ()", if I examine the query, I get the following sql (more or less)
SELECT [Extent1].* FROM [dbo].[TBLINVENTORY] AS [Extent1] INNER JOIN [dbo].[tblWS_ActiveList] AS [Extent2] ON [Extent1].[WS_ActiveList_ID] = [Extent2].[ID] WHERE ([Extent1].[SHOWIT] = 1) AND (([Extent1].[dtStart] < CAST( SysDateTime() AS datetime2)) OR ([Extent1].[dtStart] IS NULL)) AND (([Extent1].[dtEnd] > CAST( SysDateTime() AS datetime2)) OR ([Extent1].[dtEnd] IS NULL)) AND ([Extent1].[sSystem] LIKE '%OE%') AND ([Extent1].[WS_ActiveList_ID] = 0 OR [Extent2].[WS_MasterList_ID] = 16)
Unfortunately, this is not what I need, because the relationship between Inventory and ActiveList is not really 1-to-many, but Zero-to-many (I'm not sure what I use correct terms). In principle, an inventory item may or may not have a corresponding ActiveList.
If I changed this raw SQL to use LEFT OUTER JOIN , instead of INNER JOIN , SQL will return the values I expect.
What is necessary to force the LEFT INTERNAL WORK?
I tried the recommended solution from Linq for entities - From one to many relationships - you need to leave the outer join instead of the cross join , but
var q2 = from inv in Dal.TBLINVENTORies from al in inv.tblWS_ActiveList
returns an error:
Error 65 An expression of type 'xxxx.DAL.tblWS_ActiveList' is not allowed in a subsequent from clause in a query expression with source type 'System.Data.Entity.DbSet<xxxx.DAL.TBLINVENTORY>'. Type inference failed in the call to 'SelectMany'.
I wonder if the connection / relationship is not built correctly? Any other ideas?
Thanks!
EDIT :: More Information
and the definition of WS_ActiveList_ID:
[WS_ActiveList_ID] [int] NOT NULL CONSTRAINT [DF_TBLINVENTORY_WS_ActiveList_ID] DEFAULT (0),