Situation:
TableParent with 2 primaryKeys, ParentKey1 and ParentKey2
TableChild with 1 primaryKey, ChildKey
TableConnector with columns ParentKey1, ParentKey2 and ChildKey
This is where I think I should go with my Linq query. Notice I’m fetching all childs belonging to a parent so I have it’s keys as parameters.
var query = from conn in db.TableConnector
join child in db.TableChild on conn.ChildKey equals child.childKey
join par in db.TableParent on conn.ParentKey1 equals par.parentkey1 into connGroup
from co in connGroup
where co.ParentKey1 == Parameter1
Select child;
Well I tthink this works up to a point, let’s say if parent had only one key, am I right?
I guess I have to join some more into a second group but I’m currently lost.
Firstly, you don’t have two primary keys on your table. You can only have one primary key (hence the name primary). It sounds like you have a composite primary key, which means a key that is composed of more than one column.
I’m not sure what problem you are trying to solve, but it seems to be retrieving all TableChild rows for a given TableParent key. It should be something like this:
If you have your tables mapped correctly on your Linq-to-Sql designer then you don’t have to manually join them – that’s what the Linq-to-Sql code generation does for you.
For example, when you have a
TableConnectoryou will be able to retrieve theTableChildrows for it using something like this