【问题标题】:Inner query(sub query) using LINQ to Entities that returns IEnumerable Lists within Lists使用 LINQ to Entities 的内部查询(子查询)返回列表中的 IEnumerable 列表
【发布时间】:2015-04-06 13:50:18
【问题描述】:

我正在尝试从表 Article 中获取文章列表,其中包含每篇文章所在的类别列表。假设文章“A”有 3 个类别“1”、“2”、“3”,而我的文章不止一个类别。我想在 ArticleViewModel 的列表中获取 CategoryViewModel 的列表。因为它是 IEnumerableList,所以当我使用 FirstOrDefault() 时,我在每篇文章下得到相同的类别名称,但我得到的文章数量是正确的。如何在文章(列表)中获取类别名称(列表)。

ArticleViewModel 类

公共类 ArticleViewModel { 公共 int ArticleID { 获取;放; } 公共 int AuthorID { 获取;放; } 公共字符串作者姓名 { 获取;放; } 公共字符串用户名 { 获取;放; } 公共字符串蛞蝓{得到;放; } 公共字符串标题 { 获取;放; } 公共字符串 ArticleContent { 获取;放; } 公共日期时间 PostDate { 获取;放; } 公共 Nullable 更新日期 { 获取;放; } 公共 int ArticleCategoryID { 获取;放; } 公共 int 类别 ID { 获取;放; } 公共 IEnumerable 类别名称 { 获取;放; } 公共 int TagID { 获取;放; } 公共字符串 TagName { 获取;放; } }

CategoryViewModelClass

公共类 CategoryViewModel { 公共 int 类别 ID { 获取;放; } 公共字符串类别名称 { 获取;放; } }

文章类别

公共部分类 ArticleCategory { 公共 int ArticleCategoryID { 获取;放; } 公共 int ArticleID { 获取;放; } 公共 int 类别 ID { 获取;放; } 公共虚拟文章文章{获取;放; } 公共虚拟类别类别{获取;放; }

查询

result =(来自 db.Articles 中的文章
                在 db.ArticleCategories 上加入文章类别。ArticleID 等于 articlecategories.ArticleID
                在 articlecategories.CategoryID 上的 db.Categories 中加入 cat 等于 cat.CategoryID
                在文章中加入 db.Authors 中的身份验证。AuthorID 等于 auth.AuthorID
                选择新的 ArticleViewModel
                {
                    AuthorName = auth.AuthorName,
                    用户名 = auth.UserName,

                    CategoryNames =(来自 db.Articles 中的 a
                                    //来自 db.ArticleCategories 中的 ca
                                    //从c in db.Categories group c by c.CategoryName into group1
                                    在 articlecategories.ArticleID 上的 db.ArticleCategories 中加入 ca 等于 ca.ArticleID
                                    在 cat.CategoryID 上的 db.Categories 中加入 c 等于 c.CategoryID 进入 group1
                                    选择新的 CategoryViewModel
                                    {
                                        CategoryName = (从 group1 中的 group2 选择 group2.CategoryName).FirstOrDefault()
                                    // CategoryID = (from group2 in group1 select group2.CategoryID).FirstOrDefault()
                                    }),

                    标题=文章。标题,
                    蛞蝓=文章.蛞蝓,
                    文章内容 = 文章.文章内容,
                    PostDate = 文章.PostDate
                }).GroupBy(a => a.Title).Select(a => a.FirstOrDefault()).OrderByDescending(article => article.PostDate).ToList().ToPagedList(pageIndex, pageSize);

【问题讨论】:

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


    【解决方案1】:

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

    并将 LinQ 更改为

    结果 =(来自 db.Articles 中的文章 在 db.ArticleCategories 上加入文章类别。ArticleID 等于 articlecategories.ArticleID 在 articlecategories.CategoryID 上的 db.Categories 中加入 cat 等于 cat.CategoryID 在文章中加入 db.Authors 中的身份验证。AuthorID 等于 auth.AuthorID 在文章中加入 db.ArticleTags 中的标签。ArticleID 等于 tags.ArticleID 选择新的 ArticleViewModel { ArticleID=articles.ArticleID, AuthorName = auth.AuthorName, 用户名 = auth.UserName, 标题=文章。标题, 蛞蝓=文章.蛞蝓, 文章内容 = 文章.文章内容, PostDate = 文章.PostDate, CategoryNames = 从 icat inarticles.ArticleCategories 中选择 icat.Category.CategoryName, Tags = from itags in items.ArticleTags select itags.Tag.TagName }).GroupBy(a => a.Title).Select(a => a.FirstOrDefault()).ToPagedList(pageIndex, pageSize);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-09
      • 1970-01-01
      • 1970-01-01
      • 2015-05-22
      • 2021-07-11
      相关资源
      最近更新 更多