dave2118
dave2118

Reputation: 203

Linq get child from Parent.Child.child

I have a table structure similar to the following

Table1 (ID (UniqueIdentifier))
Table2 (ID,(UniqueIdentifier) Table1ID (UniqueIdentifier), Table3ID (UniqueIdentifier))
Table3 (ID (UniqueIdentifier), Desc)

Table 3 will have a unique Desc, and when I'm searching the DB for Table3.Desc, it may already exist in the Table1 Collection, but not in the database. So I want to search Table1 to see if Table3.Code == code. Then when Table1 Saves, it will cascade the saves.

I'm looking for something similar to the following:

string desc = "123";
Guid? Table3ID = from t1 in Table1.Table2.Where(t3 => t3.Desc == desc).Table3.ID;

I can't seem to pull the Table3 data and figured one of you smart posters will save me a couple hours of pulling my hair out.

Upvotes: 0

Views: 163

Answers (1)

dave2118
dave2118

Reputation: 203

Figured it out, of course after I posted this:

string desc = "123";
Table2 tbl2 = (from t2 in table1.Table2 select t2).Where(t3 => t3.Desc == desc).FirstOrDefault();
Table3 tbl3 = tbl2.Table3;
Console.WriteLine(tbl3.ID)

Upvotes: 1

Related Questions