[Table("Categories")]
public class Category
{
public Category()
{
Products = new HashSet<Product>();
}
public int CategoryId { get; set; }
public string CategoryName { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
[Table("Product")]
public class Product
{
public int ProductId { get; set; }
[Required, Display(Name = "Product Name")]
public string ProductName { get; set; }
public DateTime Created { get; set; }
public int CategoryId { get; set; }
public virtual Category Categories { get; set; }
}
以上是兩個表格,我想要顯示類別列表,包括每個類別中產品的數量。我如何使用它?如何顯示一個到多個關係表中的記錄?
歡迎太(因此) 。請閱讀[問]並考慮擴展您的問題以解釋*您想如何使用它*以及您到目前爲止所嘗試的內容,例如 - 您是否需要查詢數據庫?你試過什麼Linq-SQL不起作用? –
此問題已在[這裏]回答(http://stackoverflow.com/questions/3600940/what-linq-query-can-i-use-to-return-a-count-of-all-products-by-類別) –