C# 如何使用实体框架计算数据库中的嵌套集合
我正在制作我的第一个Asp.NETMVC应用程序-论坛系统。 我试图显示子类别中有多少帖子和线程 这是我的桌子:C# 如何使用实体框架计算数据库中的嵌套集合,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,我正在制作我的第一个Asp.NETMVC应用程序-论坛系统。 我试图显示子类别中有多少帖子和线程 这是我的桌子: public class Category { private ICollection<SubCategory> subCategories; public Category() { this.subCategories = new HashSet<SubCategory>(); } public int Id
public class Category
{
private ICollection<SubCategory> subCategories;
public Category()
{
this.subCategories = new HashSet<SubCategory>();
}
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<SubCategory> SubCategories
{
get { return this.subCategories; }
set { this.subCategories = value; }
}
}
public class SubCategory
{
private ICollection<Thread> threads;
public SubCategory()
{
this.threads = new HashSet<Thread>();
}
public int Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public int CategoryId { get; set; }
public virtual Category Category { get; set; }
public virtual ICollection<Thread> Threads
{
get { return this.threads; }
set { this.threads = value; }
}
}
public class Thread
{
private ICollection<Post> posts;
public Thread()
{
this.posts = new HashSet<Post>();
}
public int Id { get; set; }
public string Title { get; set; }
public virtual SubCategory SubCategory { get; set; }
public int SubCategoryId { get; set; }
public virtual ICollection<Post> Posts
{
get { return this.posts; }
set { this.posts = value; }
}
public string AuthorId { get; set; }
public virtual ApplicationUser Author { get; set; }
}
public class Post
{
public int Id { get; set; }
public string Content { get; set; }
public int ThreadId { get; set; }
public virtual Thread Thread { get; set; }
public string AuthorId { get; set; }
public virtual ApplicationUser Author { get; set; }
}
但这给了我所有线程和帖子的数量,而不是每个子类别的具体数量。
提前感谢。我相信您正在寻找的是:
var model = this.Data
.Categories
.Select(c => new IndexCategoryViewModel
{
Name = c.Name,
SubCategories = c.SubCategories,
ThreadsCount = c.Threads.Count(),
PostsCount = c.Threads.Sum(t => t.Posts.Count()),
})
.ToList();
在这里,您可以创建嵌套查询来统计每个类别的线程和帖子。您不需要threadscont
和postscont
,因为它们存储总计数,而不是每个类别的计数
注意
我假设Threads
属性存在于category类上,而Posts
属性存在于thread类上。否则,您将需要使用谓词将POST与线程关联,并将线程与类别关联。最常见的是ID,在这种情况下,代码看起来类似于:
var model = this.Data
.Categories
.Select(c => new IndexCategoryViewModel
{
Name = c.Name,
SubCategories = c.SubCategories,
ThreadsCount = this.Data
.Threads
.Count(t => t.CategoryId == c.Id),
PostsCount = this.Data
.Posts
.Count(p => this.Data
.Threads
.Any(t => p.ThreadId == t.Id && t.CategoryId == c.Id)
),
})
.ToList();
请注意,我还跳过了所有
.all()
调用,因为它们看起来是多余的,尽管我不确定它们是做什么的,所以如果需要,您可以将其扔回那里。试试这个,您还需要更改ViewModel以反映需求
public ActionResult Index()
{
var model = from subCategory in this.Data.SubCategories
select new SubCategoryViewModel
{
Name = subCategory.Category.Name,
SubCategory = subCategory,
ThreadsCount = subCategory.Threads.Count(),
PostsCount = subCategory.Threads.SelectMany(c => c.Posts).Count(),
})
.ToList();
return this.View(model);
}
创建表示要显示内容的视图模型
public class SubCategoryVM
{
public string Title { get; set; }
public string Description { get; set; }
public int ThreadsCount { get; set; }
public int PostCount { get; set; }
}
public class CategoryVM
{
public string Name { get; set; }
public List<SubCategoryVM> SubCategories { get; set; }
}
看法
@model IEnumerable
@{
ViewBag.Title=“主页”;
}
@foreach(模型中的var类别)
{
@类别.名称
@foreach(类别中的var子类别。子类别)
{
//在你看来,下面这句话毫无意义
//
//假设您在SubCategory Controller中有方法:public ActionResult Details(字符串标题),那么
@ActionLink(subCategory.Title,“详细信息”,“子类别”,新{Title=subCategory.Title},null)
@子类别.说明
@子类别.ThreadCountthreads
@subCategory.PostCountposts
}
}
这就是整个视图吗?如果是这样,则需要创建一个表示要显示内容的视图模型。特别是,CategoryViewModel应该包含线程计数和posts计数的属性,并且您是否真的有一个名为子类别的控制器,它对每个子类别标题都有一个方法(
)!
public ActionResult Index()
{
var model = from subCategory in this.Data.SubCategories
select new SubCategoryViewModel
{
Name = subCategory.Category.Name,
SubCategory = subCategory,
ThreadsCount = subCategory.Threads.Count(),
PostsCount = subCategory.Threads.SelectMany(c => c.Posts).Count(),
})
.ToList();
return this.View(model);
}
public class SubCategoryVM
{
public string Title { get; set; }
public string Description { get; set; }
public int ThreadsCount { get; set; }
public int PostCount { get; set; }
}
public class CategoryVM
{
public string Name { get; set; }
public List<SubCategoryVM> SubCategories { get; set; }
}
public ActionResult Index()
{
var model = this.Data.Categories.Select(c => new CategoryVM
{
Name = c.Name,
SubCategories = c.SubCategories.Select(s => new SubCategoryVM
{
Title = s.Title,
Description = s.Description,
ThreadsCount = s.Threads.Count,
PostsCount = s.Threads.SelectMany(t => t.Posts).Count;
})
});
return View(model);
}
@model IEnumerable<CategoryVM>
@{
ViewBag.Title = "Home Page";
}
<div class="container">
@foreach (var category in Model)
{
<div class="row">
<h5>@category.Name</h5>
@foreach (var subCategory in category.SubCategories)
{
<div class="col-md-10">
<div class="row">
<h7>
// The following line in your view makes no sense
// <a href="/SubCategory/@subCat.Title">@subCat.Title</a>
// Assuming you have method: public ActionResult Details(string title) in SubCategoryController, then
@Html.ActionLink(subCategory.Title, "Details", "SubCategory", new { title = subCategory.Title }, null)
</h7>
</div>
<div class="row">
<p>@subCategory.Description</p>
</div>
</div>
<div class="col-md-2">
<p><span>@subCategory.ThreadCount</span><span>threads</span></p>
<p><span>@subCategory.PostCount</span><span>posts</span></p>
</div>
}
</div>
}
</div>