Skip to content Skip to sidebar Skip to footer

Linq To Sql Foreign Keys

DDL for Database Tables: Users: id - int - identity name - varchar - unique PCs: id - int - idnetity name - varchar - unique userid - FK to Users

Solution 1:

That will work. It's normal because the second "from" inquires in the PC's set of the users and the third "from", the Apps set of the pcs. And I guess that is not going generate a cross join as T-Sql, because the conditions are already defined in the foreign keys and associations.

But I guess this should be the syntactically you want by inner join;

var query = from u in db.Users
            join p in db.PCs on p.UserId == u.Id
            join a in db.Apps on a.PCId == p.Id
            selectnew
            {
                username = u.Name,
                pcname = p.Name,
                appname = a.Name
            };

Solution 2:

Jason - I had missed the fact that you were using the built-in Linq to SQL relationships. Sorry for the misunderstanding; under normal circumstances what you have proposed here will work just fine.

Solution 3:

That looks fine to me, I'm not sure why that would create a cross join

Post a Comment for "Linq To Sql Foreign Keys"