Linq Query Timing Out - asp.net-mvc
I have this query that uses the DBContext entities I created.
var referral = entities.StudentReferrals.Where(x => x.ReferralID == p && x.SchoolYear == year).FirstOrDefault();
When I remove x.SchoolYear == year the query works fine, but with it my query times out. The opposite of what I would expect to happen, I would expect the more you narrow a query down via Where clause constraints the less likely it would time out.
SchoolYear is a field in the query and the query itself is valid, when I perform the query within SQL Studio Manager it returns results in less than a second.
My confusion is, why would adding a constraint to the Where clause cause a query to time out??
x.SchoolYear and year are both strings.
The full query is...
SELECT [Extent1].[BirthDate] AS [BirthDate],
[Extent1].[LegalFirstName] AS [LegalFirstName],
[Extent1].[LegalLastName] AS [LegalLastName],
[Extent1].[PreferredFirstName] AS [PreferredFirstName],
[Extent1].[PreferredLastName] AS [PreferredLastName],
[Extent1].[StudentNumber] AS [StudentNumber],
[Extent1].[LegacyStudentNumber] AS [LegacyStudentNumber],
[Extent1].[TranscriptSchoolCode] AS [TranscriptSchoolCode],
[Extent1].[OEN] AS [OEN],
[Extent1].[StatusIndicator] AS [StatusIndicator],
[Extent1].[SchoolYear] AS [SchoolYear],
[Extent1].[ReferralID] AS [ReferralID],
[Extent1].[PersonID] AS [PersonID],
[Extent1].[Active] AS [Active],
[Extent1].[ServiceTypeID] AS [ServiceTypeID],
[Extent1].[IsSchoolActive] AS [IsSchoolActive],
[Extent1].[Principal] AS [Principal],
[Extent1].[SchoolName] AS [SchoolName],
[Extent1].[SchoolCode] AS [SchoolCode],
[Extent1].[NearNorthSchoolCode] AS [NearNorthSchoolCode],
[Extent1].[TranscriptSchoolPrincipal] AS [TranscriptSchoolPrincipal],
[Extent1].[TranscriptSchoolName] AS [TranscriptSchoolName],
[Extent1].[TranscriptNearNorthSchoolCode] AS [TranscriptNearNorthSchoolCode],
[Extent1].[GuardianFirstName] AS [GuardianFirstName],
[Extent1].[GuardianLastName] AS [GuardianLastName],
[Extent1].[AreaCode] AS [AreaCode],
[Extent1].[ContactNo] AS [ContactNo],
[Extent1].[ReferredByFirstName] AS [ReferredByFirstName],
[Extent1].[ReferredByLastName] AS [ReferredByLastName],
[Extent1].[ReferredDate] AS [ReferredDate],
[Extent1].[Reason] AS [Reason],
[Extent1].[gender] AS [gender],
[Extent1].[grade] AS [grade],
[Extent1].[HomeroomTeacher] AS [HomeroomTeacher],
[Extent1].[IntakeTeamMember] AS [IntakeTeamMember],
[Extent1].[IntakeMemberID] AS [IntakeMemberID]
FROM (SELECT [StudentReferrals].[BirthDate] AS [BirthDate],
[StudentReferrals].[LegalFirstName] AS [LegalFirstName],
[StudentReferrals].[LegalLastName] AS [LegalLastName],
[StudentReferrals].[PreferredFirstName] AS [PreferredFirstName],
[StudentReferrals].[PreferredLastName] AS [PreferredLastName],
[StudentReferrals].[gender] AS [gender],
[StudentReferrals].[StudentNumber] AS [StudentNumber],
[StudentReferrals].[LegacyStudentNumber] AS [LegacyStudentNumber],
[StudentReferrals].[TranscriptSchoolCode] AS [TranscriptSchoolCode],
[StudentReferrals].[OEN] AS [OEN],
[StudentReferrals].[StatusIndicator] AS [StatusIndicator],
[StudentReferrals].[SchoolYear] AS [SchoolYear],
[StudentReferrals].[grade] AS [grade],
[StudentReferrals].[ReferralID] AS [ReferralID],
[StudentReferrals].[PersonID] AS [PersonID],
[StudentReferrals].[Active] AS [Active],
[StudentReferrals].[ServiceTypeID] AS [ServiceTypeID],
[StudentReferrals].[IsSchoolActive] AS [IsSchoolActive],
[StudentReferrals].[Principal] AS [Principal],
[StudentReferrals].[SchoolName] AS [SchoolName],
[StudentReferrals].[SchoolCode] AS [SchoolCode],
[StudentReferrals].[NearNorthSchoolCode] AS [NearNorthSchoolCode],
[StudentReferrals].[TranscriptSchoolPrincipal] AS [TranscriptSchoolPrincipal],
[StudentReferrals].[TranscriptSchoolName] AS [TranscriptSchoolName],
[StudentReferrals].[TranscriptNearNorthSchoolCode] AS [TranscriptNearNorthSchoolCode],
[StudentReferrals].[GuardianFirstName] AS [GuardianFirstName],
[StudentReferrals].[GuardianLastName] AS [GuardianLastName],
[StudentReferrals].[AreaCode] AS [AreaCode],
[StudentReferrals].[ContactNo] AS [ContactNo],
[StudentReferrals].[ReferredByFirstName] AS [ReferredByFirstName],
[StudentReferrals].[ReferredByLastName] AS [ReferredByLastName],
[StudentReferrals].[ReferredDate] AS [ReferredDate],
[StudentReferrals].[IntakeTeamMember] AS [IntakeTeamMember],
[StudentReferrals].[IntakeMemberID] AS [IntakeMemberID],
[StudentReferrals].[Reason] AS [Reason],
[StudentReferrals].[HomeroomTeacher] AS [HomeroomTeacher]
FROM [dbo].[StudentReferrals] AS [StudentReferrals]) AS [Extent1]
WHERE ([Extent1].[ReferralID] = #p__linq__0) AND ([Extent1].[SchoolYear] = #p__linq__1)
Here is the StudentReferral definition...
SELECT TOP (100) PERCENT p.person_id AS PersonID, p.birth_date AS BirthDate, p.legal_first_name AS LegalFirstName, p.legal_surname AS LegalLastName, p.preferred_first_name AS PreferredFirstName,
p.preferred_surname AS PreferredLastName, p.gender, p.student_no AS StudentNumber, p.legacy_student_number AS LegacyStudentNumber, p.transcript_school_code AS TranscriptSchoolCode,
p.oen_number AS OEN, s.status_indicator_code AS StatusIndicator, s.school_year AS SchoolYear, s.grade, CAST(CASE WHEN PATINDEX('%[^A-Za-z]%', s.Grade) = 0 THEN 1 ELSE CASE WHEN CAST(s.Grade AS int)
< 9 THEN 1 ELSE 0 END END AS bit) AS IsElementary, t.SchoolName, t.SchoolCode, t.NearNorthSchoolCode, pg.person_id AS GuardianID, pg.legal_first_name AS GuardianFirstName,
pg.legal_surname AS GuardianLastName, pt.area_code AS AreaCode, pt.phone_no AS ContactNo, pt.email_account AS Email
FROM Trillium.dbo.persons AS p INNER JOIN
Trillium.dbo.student_registrations AS s ON s.person_id = p.person_id INNER JOIN
dbo.Schools AS t ON t.SchoolCode = s.school_code INNER JOIN
NNDSB_AD_Routines.dbo.Students_Trillium_Guardians AS g ON s.person_id = g.student_person_id INNER JOIN
Trillium.dbo.persons AS pg ON g.contact_person_id = pg.person_id INNER JOIN
Trillium.dbo.person_telecom AS pt ON pg.person_id = pt.person_id
WHERE (s.status_indicator_code IN ('Active', 'PreReg')) AND (pt.telecom_type_name = 'home')
GROUP BY p.person_id, p.birth_date, p.legal_first_name, p.legal_surname, p.preferred_first_name, p.preferred_surname, p.gender, p.student_no, p.legacy_student_number, p.transcript_school_code, p.oen_number,
s.status_indicator_code, s.school_year, s.grade, CAST(CASE WHEN PATINDEX('%[^A-Za-z]%', s.Grade) = 0 THEN 1 ELSE CASE WHEN CAST(s.Grade AS int) < 9 THEN 1 ELSE 0 END END AS bit), t.SchoolName,
t.SchoolCode, t.NearNorthSchoolCode, pg.person_id, pg.legal_first_name, pg.legal_surname, pt.area_code, pt.phone_no, pt.email_account, g.primary_contact_priority
ORDER BY g.primary_contact_priority
I can almost guarantee that the query that EF produces and the query you're executing in SSMS are not the exact same SELECT statement. You probably wrote something like what Stephen Byrne has in his answer, i.e.
SELECT * from StudentReferrals WHERE ReferallID=1 AND SchoolYear='2015'
Right off the bat this query doesn't have a TOP qualifier on it which your EF query probably will due to the presence of the FirstOrDefault call.
Your first step should be to use something like SQL Profiler and grab the actual query that EF is generating. It's possible that with that query the optimizer is choosing to do a table scan because of the type of query that is being generated.
This likely won't make any difference, but you could also try rewriting your query as:
var referral = entities.StudentReferrals.FirstOrDefault(x => x.ReferralID == p && x.SchoolYear == year);
As an example, when I write the following query against my database:
OrganizationalNodes.FirstOrDefault(on => on.Name == "Justice League")
EF generates the following SQL:
SELECT
[Limit1].[C1] AS [C1],
[Limit1].[Id] AS [Id],
-- columns omitted for brevity
FROM ( SELECT TOP (1)
[Extent1].[Id] AS [Id],
[Extent1].[Name] AS [Name],
-- columns omitted for brevity
'0X0X' AS [C1]
FROM [dbo].[OrganizationalItems] AS [Extent1]
INNER JOIN [dbo].[OrganizationalNodes] AS [Extent2] ON [Extent1].[Id] = [Extent2].[Id]
WHERE N'Justice League' = [Extent1].[Name]
) AS [Limit1]
Well, to answer the question
why would adding a constraint to the Where clause cause a query to time out
The most likely cause is that you have a lot of data in the table, but no index covers the SchoolYear column. Therefore when you include in in a WHERE clause, this causes a Table Scan (because every row has to be checked to see if it should be included or not in the result set)
If you use SQL Server Management Studio and write the query manually for e.g
SELECT * from StudentReferrals WHERE ReferallID=1 AND SchoolYear='2015'
And then include the actual Execution Plan (Query->Include Actual Estimation Plan) then you will get the execution breakdown which will show you clearly if there is a Table Scan involved. If there is, create an index to "cover" the columns involved and it should fix your issue.
Update
Another possible solution could be to run DBCC FREEPROCCACHE to clear out any cached execution plans just in case for some reason SQL Server has picked something insane for whatever query is generated by Entity Framework.
Related
Solr pass value between subquery and upper query
I have to perform the following SQL query on a Solr collection: select c.CONTRACT from contracts c where c.LIMIT_TYPE = 4 and c.TRANSACTION_DATE_DB = (select max(TRANSACTION_DATE_DB) from contracts c2 where c.CONTRACT = c2.CONTRACT and c2.LIMIT_TYPE = 4) and c.STATUS = 'ENABLED' group by c.CONTRACT order by c.CONTRACT desc; The objective of the query is the following: the subquery find, for each contract, the record with last transaction date for limit type 4 the upper query checks if, for each contract, if the record returned has status enabled. I've seen that there are two possible ways to perform this query using solr: join and subquery Here below I'll paste my 100th attempt using subquery: http://localhost:8983/solr/contracts_collection/select? q=*:* &indent=true fq=LIMIT_TYPE:4& fq=STATUS:ENABLED &fl=*,mySub:[subquery] &mySub.q=* &mySub.fq={!term f=CONTRACT_IDENTITY v=$row.CONTRACT_IDENTITY} &mySub.fq=LIMIT_TYPE:4 &mySub.fl=CONTRACT_IDENTITY,TRANSACTION_DATE_DB,LIMIT_TYPE &mySub.sort=TRANSACTION_DATE_DB+desc &mySub.rows=1 The subquery is correct. However, for each contract, the result contains all the records for that contract. In fact, I'm missing here how to make the upper query return, for each contract, only the record having the same transaction_date_db returned by the subquery. I tried to add the following condition: &mySub.fq={!term f=TRANSACTION_DATE_DB v=$row.TRANSACTION_DATE_DB} But still, I don't have the desired result. I've also saw join, but I've failed miserably :P Does anyone have any suggestion? Going crazy for two days now. Thanks a lot
Getting Conditional Count in Join with Laravel Query Builder
I am trying to achieve the following with Laravel Query builder. I have a table called deals . Below is the basic schema id deal_id merchant_id status deal_text timestamps I also have another table called merchants whose schema is id merchant_id merchant_name about timestamps Currently I am getting deals using the following query $deals = DB::table('deals') -> join ('merchants', 'deals.merchant_id', '=', 'merchants.merchant_id') -> where ('merchant_url_text', $merchant_url_text) -> get(); Since only 1 merchant is associated with a deal, I am getting deals and related merchant info with the query. Now I have a 3rd table called tbl_deal_votes. Its schema looks like id deal_id vote (1 if voted up, 0 if voted down) timestamps What I want to do is join this 3rd table (on deal_id) to my existing query and be able to also get the upvotes and down votes each deal has received.
To do this in a single query you'll probably need to use SQL subqueries, which doesn't seem to have good fluent query support in Laravel 4/5. Since you're not using Eloquent objects, the raw SQL is probably easiest to read. (Note the below example ignores your deals.deal_id and merchants.merchant_id columns, which can likely be dropped. Instead it just uses your deals.id and merchants.id fields by convention.) $deals = DB::select( DB::raw(' SELECT deals.id AS deal_id, deals.status, deals.deal_text, merchants.id AS merchant_id, merchants.merchant_name, merchants.about, COALESCE(tbl_upvotes.upvotes_count, 0) AS upvotes_count, COALESCE(tbl_downvotes.downvotes_count, 0) AS downvotes_count FROM deals JOIN merchants ON (merchants.id = deals.merchant_id) LEFT JOIN ( SELECT deal_id, count(*) AS upvotes_count FROM tbl_deal_votes WHERE vote = 1 && deal_id GROUP BY deal_id ) tbl_upvotes ON (tbl_upvotes.deal_id = deals.id) LEFT JOIN ( SELECT deal_id, count(*) AS downvotes_count FROM tbl_deal_votes WHERE vote = 0 GROUP BY deal_id ) tbl_downvotes ON (tbl_downvotes.deal_id = deals.id) ') ); If you'd prefer to use fluent, this should work: $upvotes_subquery = ' SELECT deal_id, count(*) AS upvotes_count FROM tbl_deal_votes WHERE vote = 1 GROUP BY deal_id'; $downvotes_subquery = ' SELECT deal_id, count(*) AS downvotes_count FROM tbl_deal_votes WHERE vote = 0 GROUP BY deal_id'; $deals = DB::table('deals') ->select([ DB::raw('deals.id AS deal_id'), 'deals.status', 'deals.deal_text', DB::raw('merchants.id AS merchant_id'), 'merchants.merchant_name', 'merchants.about', DB::raw('COALESCE(tbl_upvotes.upvotes_count, 0) AS upvotes_count'), DB::raw('COALESCE(tbl_downvotes.downvotes_count, 0) AS downvotes_count') ]) ->join('merchants', 'merchants.id', '=', 'deals.merchant_id') ->leftJoin(DB::raw('(' . $upvotes_subquery . ') tbl_upvotes'), function($join) { $join->on('tbl_upvotes.deal_id', '=', 'deals.id'); }) ->leftJoin(DB::raw('(' . $downvotes_subquery . ') tbl_downvotes'), function($join) { $join->on('tbl_downvotes.deal_id', '=', 'deals.id'); }) ->get(); A few notes about the fluent query: Used the DB::raw() method to rename a few selected columns. Otherwise, there would have been a conflict between deals.id and merchants.id in the results. Used COALESCE to default null votes to 0. Split the subqueries into separate PHP strings to improve readability. Used left joins for the subqueries so deals with no upvotes/downvotes still show up.
How to convert SQL statement "delete from TABLE where someID not in (select someID from Table group by property1, property2)
I'm trying to convert the following SQL statement to Core Data: delete from SomeTable where someID not in ( select someID from SomeTable group by property1, property2, property3 ) Basically, I want to retrieve and delete possible duplicates in a table where a record is deemed a duplicate if property1, property2 and property3 are equal to another record. How can I do that? PS: As the title says, I'm trying to convert the above SQL statement into iOS Core Data methods, not trying to improve, correct or comment on the above SQL, that is beyond the point. Thank you.
It sounds like you are asking for SQL to accomplish your objective. Your starting query won't do what you describe, and most databases wouldn't accept it at all on account of the aggregate subquery attempting to select a column that is not a function of the groups. UPDATE I had initially thought the request was to delete all members of each group containing dupes, and wrote code accordingly. Having reinterpreted the original SQL as MySQL would do, it seems the objective is to retain exactly one element for each combination of (property1, property2, property3). I guess that makes more sense anyway. Here is a standard way to do that: delete from SomeTable st1 where someID not in ( select min(st2.someId) from SomeTable st2 group by property1, property2, property3 ) That's distinguished from the original by use of the min() aggregate function to choose a specific one of the someId values to retain from each group. This should work, too: delete from SomeTable st1 where someID in ( select st3.someId from SomeTable st2 join SomeTable st3 on st2.property1 = st3.property1 and st2.property2 = st3.property2 and st2.property3 = st3.property3 where st2.someId < st3.someId ) These two queries will retain the same rows. I like the second better, even though it's longer, because the NOT IN operator is kinda nasty for choosing a small number of elements from a large set. If you anticipate having enough rows to be concerned about scaling, though, then you should try both, and perhaps look into optimizations (for example, an index on (property1, property2, property3)) and other alternatives. As for writing it in terms of Core Data calls, however, I don't think you exactly can. Core Data does support grouping, so you could write Core Data calls that perform the subquery in the first alternative and return you the entity objects or their IDs, grouped as described. You could then iterate over the groups, skip the first element of each, and call Core Data deletion methods for all the rest. The details are out of scope for the SO format. I have to say, though, that doing such a job in Core Data is going to be far more costly than doing it directly in the database, both in time and in required memory. Doing it directly in the database is not friendly to an ORM framework such as Core Data, however. This sort of thing is one of the tradeoffs you've chosen by going with an ORM framework. I'd recommend that you try to avoid the need to do this at all. Define a unique index on SomeTable(property1, property2, property3) and do whatever you need to do to avoid trying to creating duplicates or to gracefully recover from a (failed) attempt to do so.
DELETE SomeTable FROM SomeTable LEFT OUTER JOIN ( SELECT MIN(RowId) as RowId, property1, property2, property3 FROM SomeTable GROUP BY property1, property2, property3 ) as KeepRows ON SomeTable.RowId = KeepRows.RowId WHERE KeepRows.RowId IS NULL
A few pointers for doing this in iOS: Before iOS 9 the only way to delete objects is individually, ie you will need to iterate through an array of duplicates and delete each one. (If you are targeting iOS9, there is a new NSBatchDeleteRequest which will help delete them all in one go - it does act directly on the store but also does some cleanup to eg. ensure relationships are updated where necessary). The other problem is identifying the duplicates. You can configure a fetch to group its results (see the propertiesToGroupBy of NSFetchRequest), but you will have to specify NSDictionaryResultType (so the results are NOT the objects themselves, just the values from the relevant properties.) Furthermore, CoreData will not let you fetch properties (other than aggregates) that are not specified in the GROUP BY. So the suggestion (in the other answer) to use min(someId) will be necessary. (To fetch an expression such as this, you will need to use an NSExpression, embed it in an NSExpressionDescription and pass the latter in propertiesToFetch of the fetch request). The end result will be an array of dictionaries, each holding the someId value of your prime records (ie the ones you don't want to delete), from which you have then got to work out the duplicates. There are various ways, but none will be very efficient. So as the other answer says, duplicates are better avoided in the first place. On that front, note that iOS 9 allows you to specify attributes that you would like to be unique (individually or collectively). Let me know if you would like me to elaborate on any of the above.
Group-wise Maximum: select t1.someId from SomeTable t1 left outer join SomeTable t2 on t1.property1 = t2.property1 and t1.property2 = t2.property2 and t1.property3 = t2.property3 and t1.someId < t2.someId where t2.someId is null; So, this could be the answer delete SomeTable where someId not in (select t1.someId from SomeTable t1 left outer join SomeTable t2 on t1.property1 = t2.property1 and t1.property2 = t2.property2 and t1.property3 = t2.property3 and t1.someId < t2.someId where t2.someId is null); Sqlfiddle demo
You can use exists function to check for each row if there is another row that exists whose id is not equal to the current row and all other properties that define the duplicate criteria of each row are equal to all the properties of the current row. delete from something where id in (SELECT sm.id FROM sometable sm where exists( select 1 from sometable sm2 where sm.prop1 = sm2.prop1 and sm.prop2 = sm2.prop2 and sm.prop3 = sm2.prop3 and sm.id != sm2.id) );
I think you could easily handle this by creating a derived duplicate_flg column and set it to 1 when all three property values are equal. Once that is done, you could just delete those records where duplicate_flg = 1. Here is a sample query on how to do this: --retrieve all records that has same property values (property1,property2 and property3) SELECT * FROM ( SELECT someid ,property1 ,property2 ,property3 ,CASE WHEN property1 = property2 AND property1 = property3 THEN 1 ELSE 0 END AS duplicate_flg FROM SomeTable ) q1 WHERE q1.duplicate_flg = 1; Here is a sample delete statement: DELETE FROM something WHERE someid IN ( SELECT someid FROM ( SELECT someid ,property1 ,property2 ,property3 ,CASE WHEN property1 = property2 AND property1 = property3 THEN 1 ELSE 0 END AS duplicate_flg FROM SomeTable ) q1 WHERE q1.duplicate_flg = 1 );
Simply, if you want to remove duplicate from table you can execute below Query : delete from SomeTable where rowid not in ( select max(rowid) from SomeTable group by property1, property2, property3 )
if you want to delete all duplicate records try the below code WITH tblTemp as ( SELECT ROW_NUMBER() Over(PARTITION BY Property1,Property2,Property3 ORDER BY Property1) As RowNumber,* FROM Table_1 ) DELETE FROM tblTemp where RowNumber >1 Hope it helps
Use the below query to delete the duplicate data from that table delete from SomeTable where someID not in (select Min(someID) from SomeTable group by property1+property2+property3)
Why does Entity Framework generate large parameters? How can they be reduced?
In a very simple db query: _service.GetAll<Visitor>().Any(r => r.EmailAddress == email) when tracing this using Glimpse, it shows a parameter #p__linq__0 of Type String and Size 4000 being passed to the database. The following SQL is generated: SELECT CASE WHEN ( EXISTS (SELECT 1 AS [C1] FROM [dbo].[Visitor] AS [Extent1] INNER JOIN [dbo].[VisitorType] AS [Extent2] ON [Extent1].[Id] = [Extent2].[Id] WHERE ([Extent2].[EmailAddress] = 'test#test.com' /* #p__linq__0 */) OR (([Extent2].[EmailAddress] IS NULL) AND ('test#test.com' /* #p__linq__0 */ IS NULL)) )) THEN cast(1 as bit) ELSE cast(0 as bit) END AS [C1] FROM ( SELECT 1 AS X ) AS [SingleRowTable1] This seems quite wasteful, when a MaxLength of 254 has been defined. Can anyone explain why this is? And how I can change the parameter size? UPDATE The following similar question also doesn't give a solution on how to change this default behaviour: Why does code first/EF use 'nvarchar(4000)' for strings in the raw SQL command? From a db optimization point of view it makes sense, however when trying to reduce the amount data transferred to mobile clients in a slow network environment, it doesn't make sense to increase every string parameter to 4000 bytes.
LINQ to entity, wrong join type
I have a query that looks like so.... 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(); Immediately before the "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 relationship between "Inventory" and "ActiveList" is not really 1-to-Many, but Zero-to-Many (I'm not sure I'm using the correct terms). Basically, An inventory item might or might not have a related "ActiveList". If I change that raw SQL to use a LEFT OUTER JOIN, instead of an INNER JOIN, the SQL returns the values I expect. What is needed to force the LEFT OUTER JOIN? I've tried the recommended solution from Linq to entities - One to many relationship - need left outer join instead of 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 my link/relationship is constructed incorrectly? Any other ideas? Thanks! EDIT :: Additional Data -- create foreign key, but don't enforce on existing values ALTER TABLE [dbo].[tblInventory] --the ONE Table WITH NOCHECK ADD CONSTRAINT [FK__tblInventory.WS_ActiveList_ID__tblWS_ActiveList.ID] FOREIGN KEY([WS_ActiveList_ID]) REFERENCES [dbo].[tblWS_ActiveList] ([ID]) --the MANY Table NOT FOR REPLICATION GO -- disable enforcement of the foreign key, but leave it in place (virtual key) ALTER TABLE [dbo].[tblInventory] NOCHECK CONSTRAINT [FK__tblInventory.WS_ActiveList_ID__tblWS_ActiveList.ID] GO and the definition of WS_ActiveList_ID: [WS_ActiveList_ID] [int] NOT NULL CONSTRAINT [DF_TBLINVENTORY_WS_ActiveList_ID] DEFAULT (0),
Your main problem is that you've turned off the referential integrity checks in your database. Apart from the obvious problem of bad data, this won't work with EF. By far the best option is to make WS_ActiveList_ID nullable, update your data to change all the 0s to NULLs and turn the constraint back on. If you can't do that, I think you'll have to generate a SQL statement as a string and execute it with dbContext.Database.SqlQuery<T> ( MSDN )