我很难想出一个好头衔。我正在使用asp.net和Entity Framework开发MVC应用程序。我有2个型号,分类和产品。每个类别中可以有许多产品。我有一个页面列出了每个类别和每个产品以及每个产品的相关字段。我想在“类别”列表中添加一个列(字段),列出该类别中的产品数量。我不知道该怎么做。我猜我需要在Categories模型中添加一个名为“productCount”的附加变量或类似的东西。
类别模型:
public class CategoryModel
{
public int ID { get; set; }
[Required(ErrorMessage = "Required")]
[Display(Name = "Category Name")]
[MaxLength(50)]
public String categoryName { get; set; }
[DefaultValue(true)]
[Display(Name = "Active?")]
public bool isActive { get; set; }
}
产品型号:
public class ProductModel
{
public int ID { get; set; }
[Required(ErrorMessage = "Required")]
[Index("ItemNumber", 1, IsUnique = true)]
[Display(Name = "Item #")]
public int itemNumber { get; set; }
[Required(ErrorMessage = "Required")]
[Display(Name = "Product")]
[MaxLength(50)]
public String product { get; set; }
[Required(ErrorMessage = "Required")]
[Display(Name = "Description")]
[MaxLength(500)]
public String description { get; set; }
[DefaultValue(true)]
[Display(Name = "Active?")]
public bool active { get; set; }
[Display(Name = "Image Name")]
public String imageName { get; set; }
[Display(Name = "PDF Name")]
public String PDFName { get; set; }
[ForeignKey("Category")]
public int CategoryID { get; set; }
public virtual CategoryModel Category { get; set; }
public IEnumerable<SelectListItem> CategoryList { get; set; }
public static IEnumerable<SelectListItem> getCategories()
{
using (var db = new ProductContext())
{
List<SelectListItem> list = new List<SelectListItem>();
var x = db.Categories.ToList();
foreach (var y in x)
{
list.Add(new SelectListItem { Value = y.ID.ToString(), Text = y.categoryName });
}
return list;
}
}
}
管理模式:
public class AdminModel
{
public IEnumerable<CategoryModel> Categories { get; set; }
public IEnumerable<ProductModel> Products { get; set; }
public RegisterViewModel RegisterUsers { get; set; }
}
以下是列出类别的Controller方法:
public ActionResult ControlPanel()
{
ViewBag.Message = TempData["Message"] == null ? "" : TempData["Message"].ToString();
//using (var db = new )
using (var db = new ProductContext())
{
var categories = from c in db.Categories
select c;
categories = categories.OrderByDescending(c => c.isActive);
var model = new AdminModel
{
Categories = categories.ToList(),
Products = db.Products.ToList()
};
return View(model);
}
}
答案 0 :(得分:1)
对于单个categoryID,您只需使用Linq的Count():
int specificCategoryID = 15;
return db.Products.Where(w => w.CategoryID == specificCategoryID).Count();
有关categoryID的键/值对列表和产品数量,请执行以下操作:
var products = db.Products.AsEnumerable();
var productCount = products.GroupBy(p => p.CategoryID,
(k, v) => new { CategoryID = k, ProductCount = v.Count() });
我建议使用正确的viewmodel并确保它只使用一个查询,这样就不会遇到选择n + 1问题。
编辑: 假设您始终拥有所有类别和产品的列表,您可以在管理视图模型中对已从数据库中提取的项目进行计算:
public class AdminModel
{
public IEnumerable<CategoryModel> Categories { get; set; }
public IEnumerable<ProductModel> Products { get; set; }
public RegisterViewModel RegisterUsers { get; set; }
public int GetProductCountForCategoryID(int categoryID)
{
return this.Products
.Count(w => w.CategoryID == categoryID);
}
}
然后在视图中,只需传入类似这样的类别ID:
@Model.GetProductCountForCategoryID(categoryID);