8,494
社区成员




public class category {
[Key]
public int CategoryId { get; set; }
[DisplayName("耗材分类名称")]
public string Title { get; set; }
//分类下数量 不生成字段
[NotMapped]
public int Count { get; set; }
}
public class product {
[Key]
public int productId { get; set; }
public int? CategoryId { get; set; }
}
select * from category
var Categories = from s in db.Categories
select s;
SELECT Category.* ,
(SELECT COUNT(*)
FROM product
WHERE product.CategoryId=Category.CategoryId) as Count
FROM Category
var query= from c in db.Categories
select new
{
c,
Count=db.Products.Where(p=>p.CategoryId==c.CategoryId).Count()
};