views:

38

answers:

1

If I have Table3 that has a FK pointing to Table2 which has a FK pointing to Table1 what I'm seeing via intellisense is I can reference Table2 from Table3 but I can't go Table3.Table2.Table1 it only goes one layer deep.

from t3 in Table3
where t3.t2.property == "some value" && t3.t2.t1.property == "some other value"
select t3.t2.t1;

This is esentially what I want to do but I can only reference t2, but not the t1 that t2 has a link to.

Should I do this:

from t3 in Table3
from t1 in Table1
where t3.t2.property == "some value" && t1.property == "some other value"
select t1;

?

+3  A: 

You can join all the tables:

from t3 in Table3
join t2 in Table2 on t3.Table2_FK equals t2.ID
join t1 in Table1 on T2.Table1_FK equals t1.ID
where t2.property == "some value" && t1.property == "some other value"
select t1;

(edit)

I doesn't go just one level deep. In fact, your first example should work. Of course, your relations must be N to 1:

Table3 (n) --- (1) Table2 (n) --- (1) Table1

given t3 of Table3 you can do:

t3.Table2.Table1

Do you have the proper connection between Table2 and Table1 in the .dbml file?

bruno conde
I thought the 2nd way I showed does the same thing because LINQ to SQL is aware of the FK relationship already. Is that incorrect?
jamone
As far as I can tell I do have Table3 (n) --- (1) Table2 (n) --- (1) Table1 set up. Shouldn't the fact that I can do t3.t2 and t2.t1 independently show that the relationships are set up properly?
jamone
Ahh I don't know why it didn't bite me before. My link between Table2 and Table1 was reversed. It was Table2 (1) --- (n) Table1.
jamone