Sql – LINQ2SQL – Cross join emitted when I want inner join

linqlinq-to-sqlsql-server-2008

This emits inner joins, which is what I want and works:

var q =
    from itm in esdc.items
    join itmImg in esdc.itemImages on itm.itemId equals itmImg.itemId
    join itmIdent in esdc.itemIdentities on itm.imgIdentityId equals itmIdent.itemIdentityId
    join startImgs in esdc.vStartPgImgs on itmImg.imgId equals startImgs.imgId
    select ...

This works too, but cross joins were emitted. How do I get inner joins? I double checked the entities and the relationships are correct.

var q =
    from itmIdent in esdc.itemIdentities
    from itm in itmIdent.items
    from itmImg in itm.itemImages
    join startImgs in esdc.vStartPgImgs 
        on itmImg.imgId equals startImgs.imgId
    select ...

If you need me to post the emitted SQL and the entity setup, let me know.

Best Answer

To put it simply: you get inner joins if you use the first form. Multiple from clauses represent cross joins (sort of, anyway 1) in LINQ.

Basically, if you want inner joins, use multiple join clauses.


1 (I say sort of because the subsequent sequences can depend on the earlier ones... but for LINQ to SQL they're almost always cross joins.)

Related Topic