Linq 对于使用实体框架的内部类,在视图模型中使用foreach进行PartialView或enumerate?
我正在创建(我的第一个)真正简单的ASP.NET MVC博客网站,我想知道什么是按最新日期时间对我的评论进行排序的最佳方法,以及如何使用LINQ将查询结果注入到视图中,还是我需要IQueryable PartialView 我一直在读IEnumerable和IQueryable的比较,我想我不想把这些评论记在记忆里,除非我对它们进行过滤和排序 我在想一个使用@model Queryable的PartialView,在这里我使用ViewBag将内部类传递给[ChildAction],或者我可以在视图中使用foreach循环吗 我的文章类看起来有点像这样:Linq 对于使用实体框架的内部类,在视图模型中使用foreach进行PartialView或enumerate?,linq,entity-framework,ienumerable,iqueryable,asp.net-mvc-partialview,Linq,Entity Framework,Ienumerable,Iqueryable,Asp.net Mvc Partialview,我正在创建(我的第一个)真正简单的ASP.NET MVC博客网站,我想知道什么是按最新日期时间对我的评论进行排序的最佳方法,以及如何使用LINQ将查询结果注入到视图中,还是我需要IQueryable PartialView 我一直在读IEnumerable和IQueryable的比较,我想我不想把这些评论记在记忆里,除非我对它们进行过滤和排序 我在想一个使用@model Queryable的PartialView,在这里我使用ViewBag将内部类传递给[ChildAction],或者我可以在视
public class Article
{
//...more properties
public virtual IQueryable<Comment> Comments { get; set; }
}
public class Comment
{
[Key]
public int CommentId { get; set; }
[ForeignKey("Article")]
public int ArticleId { get; set; }
[DataType(DataType.DateTime), Timestamp,ScaffoldColumn(false)]
public DateTime Timestamp { get; set; }
//...more properties
public virtual Article Article { get; set; }
}
@model IQueryable<BlogSite.Models.Comment>
<table>
<tbody>
@foreach (BlogSite.Models.Comment comment in ViewBag.Comments)
{
<tr>
<td>
@...
如果我真的实现了PartialView,它看起来会有点像这样:
public class Article
{
//...more properties
public virtual IQueryable<Comment> Comments { get; set; }
}
public class Comment
{
[Key]
public int CommentId { get; set; }
[ForeignKey("Article")]
public int ArticleId { get; set; }
[DataType(DataType.DateTime), Timestamp,ScaffoldColumn(false)]
public DateTime Timestamp { get; set; }
//...more properties
public virtual Article Article { get; set; }
}
@model IQueryable<BlogSite.Models.Comment>
<table>
<tbody>
@foreach (BlogSite.Models.Comment comment in ViewBag.Comments)
{
<tr>
<td>
@...
然后我在我的ArticleRepository中创建了一个方法,从控制器中实例化的UnitOfWOrk调用它:
public IEnumerable<Comment> GetCommentsByArticleId(int id)
{
List<Comment> tempList = new List<Comment>();
var article = GetArticleById(id);
var comments = article.Comments.Where(c => c.ParentCommentId == null).OrderByDescending(c => c.Timestamp);
foreach (var c in comments)
{
if (c.isRoot)
{
tempList.Add(c);
// Replies
foreach (var r in article.Comments.Where(x => x.ParentCommentId == c.CommentId).OrderBy(x => x.Timestamp))
{
tempList.Add(r);
}
}
}
return tempList;
}
var comments = context.GetComments().ToList();
comments.Sort((x, y) => y.Timestamp.CompareTo(x.Timestamp));
return View(comments)
public IEnumerable GetCommentsByArticleId(int-id)
{
列表模板列表=新建列表();
var article=GetArticleById(id);
var comments=article.comments.Where(c=>c.ParentCommentId==null).OrderByDescending(c=>c.Timestamp);
foreach(注释中的var c)
{
如果(c.isRoot)
{
圣殿骑士。添加(c);
//答复
foreach(article.Comments.Where(x=>x.ParentCommentId==c.CommentId.OrderBy(x=>x.Timestamp))中的var r)
{
圣殿骑士。加(r);
}
}
}
返回圣殿骑士;
}
我的comment类中有一个名为bool isRoot
的属性,因此我可以呈现属性int ParentCommentId
中的所有注释,以便用户可以响应这些注释。我现在的问题是,如果我的web应用程序有数千篇文章,并且查询是在内存中完成的,而不是在数据库中完成的,那么这最终会变成性能问题吗
一旦临时列表超出范围,它不会进入垃圾收集吗?在这种情况下使用IQueryable有什么好处?使用ViewBag是一种不好的做法。如果需要排序列表,请在控制器中执行:
public IEnumerable<Comment> GetCommentsByArticleId(int id)
{
List<Comment> tempList = new List<Comment>();
var article = GetArticleById(id);
var comments = article.Comments.Where(c => c.ParentCommentId == null).OrderByDescending(c => c.Timestamp);
foreach (var c in comments)
{
if (c.isRoot)
{
tempList.Add(c);
// Replies
foreach (var r in article.Comments.Where(x => x.ParentCommentId == c.CommentId).OrderBy(x => x.Timestamp))
{
tempList.Add(r);
}
}
}
return tempList;
}
var comments = context.GetComments().ToList();
comments.Sort((x, y) => y.Timestamp.CompareTo(x.Timestamp));
return View(comments)
并在视图中传递已排序的列表:
@model IEnumerable<BlogSite.Models.Comment>
<table>
....
@model IEnumerable
....
我有一种感觉,你会这么说ViewBag,因为我读到了很多关于这三个TempData、ViewBag和viewData的坏处。看起来您建议从数据库中获取注释并在内存中进行排序。那么我如何将注释传递给[ChildAction],或者将内部类元素传递给局部视图时会发生什么?再次表示感谢!