Pradeep
Pradeep

Reputation: 4872

Need help to validate existing group of records

I have a datatable Table called Table1 with primary key PId and child table called Table2 with PId as foreign key. My table structure is like

Table1
---

PId ,  Data1,

1 ,    ABS,

2  ,   DER,


Table2
---

TId,     PId,    Cid ,   Data2,

3 ,      1,     6 ,     FR,

4 ,      1  ,    66,  RE,

I need to fetch Cid from Table2 based on the Table1 Pid. Means For Pid 1 need to fetch 6 and 66.

Can anybody please help me to do it using LINQ or any c# code. Please dont mind, i dont know how to format the above data in stack overflow.

Regards

Pradeep

Upvotes: 0

Views: 48

Answers (2)

Jen Grant
Jen Grant

Reputation: 2074

This would join the tables and select from table 2

        var results = from t1 in DataContext.Table1
                      join t2 in DataContext.Table2 on t1.Pid equals t2.Pid
                      where t1.Pid == 1
                      select t2.Cid;

Upvotes: 1

Lajos Arpad
Lajos Arpad

Reputation: 76787

If you have foreign key relations you can use a DataLoadOption object and you can call its loadWith method, you can read further information here.

If you don't have foreign key relations, you can use a join in your Linq query. An example for joins can be found here.

Upvotes: 0

Related Questions