Yogesh
Yogesh

Reputation: 1216

Linq to sql to get the records from parent-child tables

I have 2 tables (say parent and child table and 1 to many relation), i drag and droped these tables on .DBML (linq to sql classes). now in the code, i would be able to see the relation in classes as association. parent class has list of child objects. my question is can i write a linQ statement to featch a (single) record from parent table and all related records from child table and bind to an object.

 Thanks

Upvotes: 0

Views: 1369

Answers (1)

Klaus Byskov Pedersen
Klaus Byskov Pedersen

Reputation: 120917

You don't have to do anything special. Relations are loaded automatically. If you want to iterate over all child objects of a parent object you can do it like so:

var parent = objectContext.Parents.First();

foreach(var child in parent.Children)
{
    ...
}

Upvotes: 1

Related Questions