开发者

How to retrieve the records from more than one table which has one to many relationship?

开发者 https://www.devze.com 2023-04-10 05:06 出处:网络
How to retrieve the records from more than one table which has one to many relationship. Categories[table]

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号