8,492
社区成员
发帖
与我相关
我的任务
分享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 Categoryvar query= from c in db.Categories
select new
{
c,
Count=db.Products.Where(p=>p.CategoryId==c.CategoryId).Count()
};