2011-10-04 74 views
0

如何從多個表中檢索具有一對多關係的記錄。如何從多個表中檢索具有一對多關係的記錄?

Categories[table] 
CategoryId 
CategoryName 

Products[table] 
ProductId 
CategoryId 
ProductName 
Description 

的entites

Category[Entity] 
CategoryId 
CategoryName 
List<Product> 

Product[Entity] 
ProductId 
ProductName 
Description 

所以,如果我給的categoryId,我應該得到的類別細節與同類別相關的產品清單。

如何在linq to sql中做到這一點?

回答

1

在LINQ to SQL中你在每個實體生成的參考屬性。這說如果你這樣做:

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 
} 

你會得到所有的產品。

相關問題