[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-类别) –