join - LINQ to Entities joining on instance rather than id generates nasty SQL -


can explain why join entity rather id generates ugly sql when conceptually doing you'd think same thing? e.g.

by id

from companydirector in companydirectors join contactaddress in contactaddresses   on companydirector.contactaddress.id equals contactaddress.id select new {companydirector, contactaddress} 

generates

from  [company] [extent1]     inner join [address] [extent2] on [extent1].[contact_address_id] = [extent2].[contact_address_id] 

by instance

from companydirector in companydirectors join contactaddress in contactaddresses   on companydirector.contactaddress equals contactaddress select new {companydirector, contactaddress} 

generates

from  [company] [extent1] inner join [address] [extent2] on  exists (select      1 [c1]        ( select 1 x ) [singlerowtable1]     left outer join  (select          [extent3].[contact_address_id] [contact_address_id]         [address] [extent3]         [extent1].[contact_address_id] = [extent3].[contact_address_id] ) [project1] on 1 = 1     left outer join  (select          [extent4].[contact_address_id] [contact_address_id]         [address] [extent4]         [extent1].[contact_address_id] = [extent4].[contact_address_id] ) [project2] on 1 = 1     [project1].[contact_address_id] = [extent2].[contact_address_id] ) 

that looks pretty inefficient me, forcing id route. why doing left join twice, never mind once??

i can't in minds or code of ado.net team. said, see 2 possible issues:

  1. possibly, id field in underlying table in contractaddresses, or possibly in entity model, not defined primary key. doubt problem, it's worth double-checking.
  2. the equals keyword may not have way compare equality between 2 objects in join. in quick web search, did not find exactly equals uses comparison, this msdn how-to leads me believe equals , gethashcode methods involved (even if composite keys not involved). if using default object.equals inherited method, linq provider has figure out reference equality somehow, imagine lead strange results.

i solution @craig stuntz in comment, though. also, might want execution plan longer query see if it's bad looks; query optimizer might better job code indicate.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -