内部查询(子查询)使用LINQ to Entities返回列表中的IEnumerable列表

时间:2015-02-06 12:05:46

标签: c# asp.net-mvc linq-to-entities subquery inner-query

我正在尝试从表格文章获取文章列表,其中列出了每篇文章的列表。说文章" A"有3个类别" 1"," 2"," 3"我有不止一篇文章有​​多个类别。我想获取 ArticleViewModel 列表中的 CategoryViewModel 列表。因为它是IEnumerableList,当我使用FirstOrDefault()时,我在每篇文章下都得到相同的类别名称,但是我得到了正确数量的文章。我怎样才能在文章(列表)中获得类别名称(列表)。

ArticleViewModel类

    public class ArticleViewModel
    {
        public int ArticleID { get; set; }
        public int AuthorID { get; set; }
        public string AuthorName { get; set; }
        public string Username { get; set; }
        public string Slug { get; set; }
        public string Title { get; set; }
        public string ArticleContent { get; set; }
        public DateTime PostDate { get; set; }
        public Nullable UpdatedDate { get; set; }
        public int ArticleCategoryID { get; set; }
        public int CategoryID { get; set; }
        public IEnumerable CategoryNames { get; set; }
        public int TagID { get; set; }
        public string TagName { get; set; }
    }

CategoryViewModelClass

    public class CategoryViewModel
    {
        public int CategoryID { get; set; }
        public string CategoryName { get; set; }
    }

ArticleCategory

 public partial class ArticleCategory
    {
        public int ArticleCategoryID { get; set; }
        public int ArticleID { get; set; }
        public int CategoryID { get; set; }

        public virtual Article Article { get; set; }
        public virtual Category Category { get; set; }

QUERY

result = (from articles in db.Articles
                join articlecategories in db.ArticleCategories on articles.ArticleID equals articlecategories.ArticleID
                join cat in db.Categories on articlecategories.CategoryID equals cat.CategoryID
                join auth in db.Authors on articles.AuthorID equals auth.AuthorID
                select new ArticleViewModel
                {
                    AuthorName = auth.AuthorName,
                    Username = auth.UserName,

                    CategoryNames = (from a in db.Articles
                                    //from ca in db.ArticleCategories
                                    //from c in db.Categories group c by c.CategoryName into group1
                                    join ca in db.ArticleCategories on articlecategories.ArticleID equals ca.ArticleID
                                    join c in db.Categories on cat.CategoryID equals c.CategoryID into group1                                               
                                    select new CategoryViewModel
                                    {                                                  
                                        CategoryName = (from group2 in group1 select group2.CategoryName).FirstOrDefault()
                                    //   CategoryID = (from group2 in group1 select group2.CategoryID).FirstOrDefault()
                                    }),

                    Title = articles.Title,
                    Slug = articles.Slug,
                    ArticleContent = articles.ArticleContent,
                    PostDate = articles.PostDate
                }).GroupBy(a => a.Title).Select(a => a.FirstOrDefault()).OrderByDescending(article => article.PostDate).ToList().ToPagedList(pageIndex, pageSize);

1 个答案:

答案 0 :(得分:0)

好的,过了几天。我意识到这是试图让事情过度。我的错 !修复很简单;在视图模型中将类别更改为IEnumerable<string> Category {get; set;}

并将LinQ更改为

result = (from articles in db.Articles
                              join articlecategories in db.ArticleCategories on articles.ArticleID equals articlecategories.ArticleID
                              join cat in db.Categories on articlecategories.CategoryID equals cat.CategoryID
                              join auth in db.Authors on articles.AuthorID equals auth.AuthorID
                              join tags in db.ArticleTags on articles.ArticleID equals tags.ArticleID

                              select new ArticleViewModel
                              {
                                  ArticleID=articles.ArticleID,
                                  AuthorName = auth.AuthorName,
                                  Username = auth.UserName,
                                  Title = articles.Title,
                                  Slug = articles.Slug,
                                  ArticleContent = articles.ArticleContent,
                                  PostDate = articles.PostDate,
                                  CategoryNames = from icat in articles.ArticleCategories select icat.Category.CategoryName,
                                  Tags = from itags in articles.ArticleTags select itags.Tag.TagName

                              }).GroupBy(a => a.Title).Select(a => a.FirstOrDefault()).ToPagedList(pageIndex, pageSize);