目前,我有一个Comic模型,可以返回到以下视图:
var db = new ComicEntities();
var Comic = from s in db.Comics
.Where(i => i.Comic_Id == comicId)
.Include(i => i.ComicAuthors)
.Include("ComicAuthors.User")
.Include(i => i.ComicCategories)
.Include("ComicCategories.Category")
//.Include(i => i.ComicAltTitles)
.Include(i => i.Chapters)
.Include("Chapters.Pages")
//.Include(i => i.ComicRatings)
.Include(i => i.ComicReviews)
.Include("ComicReviews.ComicRating")
.Include("ComicReviews.User")
select s;
我还有一个替代方案:
var db = new ComicEntities();
var Comic = from s in db.Comics
.Where(i => i.Comic_Id == comicId)
.Include(i => i.ComicAuthors)
.Include("ComicAuthors.User")
.Include(i => i.ComicCategories)
.Include("ComicCategories.Category")
//.Include(i => i.ComicAltTitles)
.Include(i => i.Chapters)
.Include("Chapters.Pages")
//.Include(i => i.ComicRatings)
.Include(i => i.ComicReviews)
.Include("ComicReviews.ComicRating")
.Include("ComicReviews.User")
select new ComicIndexView {
comicLeftNavigationModel = new ComicLeftNavigationModel {
chaptersCount = s.Chapters.Count(),
pagesCount = s.Chapters.SelectMany(i => i.Pages).Count(),
authors = s.ComicAuthors.Select(i => i.User.UserName).ToList(),
genres = s.ComicCategories.Select(i => i.Category.Name).ToList(),
isAuthor = s.ComicAuthors.Where(i => i.User_Id == WebSecurity.CurrentUserId).Any(),
image = s.ComicImage,
published = s.DatePublished,
status = s.Completed,
ratingCount = s.ComicRatings.Count(),
ratingNumber = s.ComicRatings.Sum(i => i.Rating) / s.ComicRatings.Count()
},
comicIndexModel = new ComicIndexModel {
chapters = s.Chapters,
description = s.Description,
reviews = s.ComicReviews
}
};
我想知道哪种方法会给我最好的表现?我把它放在哪个方面真的很重要吗?什么是获得最佳性能的最佳方法?最好使用Context.ComicRatings.Where(i => i.UserId == 1)
之类的单个查询而不是使用包含吗?