How to retrieve the records from more than one table which has one to many relationship.
Categories[table]
CategoryId
CategoryName
Products[table]
ProductId
CategoryId
ProductName
Description
Entites
Category[Entity]
CategoryId开发者_Python百科
CategoryName
List<Product>
Product[Entity]
ProductId
ProductName
Description
So if i give categoryId, i should get the category details with list of products associated with the category.
How to do this in linq to sql?
In linq to sql you get a reference property generated in each of your entities. This said if you do this:
Category cat = context.Categories.FirstOrDefault(x=>x.CategoryId == 1); //Where one is the //id of a random category
foreach(Product prd in cat.Products)
{
//do some logic here
}
you will get all the products.
See Include for LINQ to SQL
精彩评论