简体   繁体   English

如何在asp.net mvc EF代码中使用linq查询关系1:N和N:N(在3个实体之间)从数据库中获取数据?

[英]how to fetch data from database using linq query for relationship 1:N and N:N (between 3 entity) in asp.net mvc EF code first?

i have a linq query for fetch data from 3 table (Post, Category, Tag). 我有一个linq查询,用于从3个表(Post,Category,Tag)中获取数据。

i want show latest posts (10 num) in default page of a blog: post title, post tags name and post category name, post short description. 我想在博客的默认页面中显示最新帖子(10位数):帖子标题,帖子标签名称和帖子类别名称,简短说明。 i type this linq query 我键入此linq查询

List<Post> IBlogRepository.PostsForList(int pageNo, int pageSize)
    {
        using (var context = new MJBweblogContext())
        {
            IQueryable posts = from Post in context.Posts
                        where Post.Published == true
                        select new
                        {
                            Post.Title,
                            Post.ShortDescription,
                            Post.Description,
                            Post.MetaData,
                            Post.PostedOn,
                            Post.UrlSlug,
                            Post.Published,
                            Post.Modified,
                            Post.Category,
                            Post.Tags,
                            Post.Category.Name,
                        };
            if (posts != null)
            {
                return posts.OfType<Post>() // i give error if i remove OfType<Post>()
                    .OrderByDescending(p => p.PostedOn)
                    .Skip(pageNo * pageSize)
                    .Take(pageSize)
                    .ToList();
            }
            else
            {
                return null;
            }
        }
    }

but i have two problem: 但我有两个问题:

  1. if i remove OfType() VS2017 tell me: 如果我删除OfType()VS2017告诉我:

    Error CS0266 Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.List'. 错误CS0266无法将类型“ System.Linq.IQueryable”隐式转换为“ System.Collections.Generic.List”。 An explicit conversion exists (are you missing a cast?) MJBweblog C:\\Users\\mjb\\source\\repos\\MJBweblog\\MJBweblog\\DAL\\BlogRepository.cs 44 Active 存在显式转换(是否缺少强制转换?)MJBweblog C:\\ Users \\ mjb \\ source \\ repos \\ MJBweblog \\ MJBweblog \\ DAL \\ BlogRepository.cs 44活动

    1. I don't know what code I should write to fetch tags for each post and their category? 我不知道我应该写什么代码来获取每个帖子及其类别的标签? in the linq query? 在linq查询中? (i try to show user something like this template of each post in default page ) (我尝试向用户显示默认页面中每个帖子的模板

this is What I want to show for users in default page. 这是我要在默认页面中显示给用户的内容。 (a list of this) (清单)

像这样的某人

If your Post class is an entity class then you don't need to create another anonymous type with select. 如果您的Post类是实体类,则无需使用select创建另一个匿名类型。 Below Linq chain should work for what you are trying to achieve. 下面的Linq链应该可以帮助您实现目标。 Also as per the comments - it is not a good practice to return null for a list, the caller can check the Count property to see if the list is empty. 同样根据注释-列表返回null并不是一个好习惯,调用者可以检查Count属性以查看列表是否为空。

        using (var context = new MJBweblogContext())
        {
            return context.Posts.Where(post => post.Published)
                .OrderByDescending(p => p.PostedOn)
                .Skip(pageNo * pageSize)
                .Take(pageSize)
                .ToList();
        }

You can't just skip all of that other stuff? 您不能只跳过所有其他内容吗?

List<Post> IBlogRepository.PostsForList(int pageNo, int pageSize)
    {
        using (var context = new MJBweblogContext())
        {
            return context.Posts.Where(a => a.Published == true)
                                .OrderByDescending(p => p.PostedOn)
                                .Skip(pageNo * pageSize)
                                .Take(pageSize)
                                .ToList();
        }
    }

if you're not getting your category information, you should be able to use INCLUDE 如果您没有获得类别信息,则应该可以使用INCLUDE

List<Post> IBlogRepository.PostsForList(int pageNo, int pageSize)
    {
        using (var context = new MJBweblogContext())
        {
            return context.Posts.Include("Category").Where(a => a.Published == true)
                                .OrderByDescending(p => p.PostedOn)
                                .Skip(pageNo * pageSize)
                                .Take(pageSize)
                                .ToList();
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 EF Core 代码优先迁移为 ASP.NET Core MVC 配置 N 层架构 - How to configure N-tier architecture for ASP.NET Core MVC with EF Core code first migrations 如何使用LINQ查询数据库以基于ASP.Net MVC 5中的月份数组从数据库中获取数据? - How to query database using LINQ to bring data from database based on array of months in ASP.Net MVC 5? 如何使用ASP.net MVC从数据库中获取数据? - How to fetch data from database using ASP.net MVC? EF代码中的模型n - n关系如何自动生成视图正常工作? - How Model n--n relationship in EF Code First to automatically generated views work correctly? 如何根据两个表之间的关系从下拉列表中插入数据库值? 我正在使用 ASP.NET c# 实体框架,代码优先 - How can I insert in database values from dropdown list based on a relationship between 2 tables? I am using ASP.NET c# Entity framework, Code first EF6 - 代码优先 - 如何正确处理从m:n关系中删除父条目 - EF6 - code-first - how to properly handle deleting a parent entry from an m:n relationship ASP.NET MVC 5-从数据库选择数据的LINQ查询 - ASP.NET MVC 5 - LINQ Query to Select Data from Database 如何使用首先通过代码创建的数据库中的数据填充谷歌图表 - ASP.Net MVC - How to populate google charts using data from database created via code first - ASP.Net MVC 在Code First EF中将N重命名为N表 - Renaming N to N table in Code First EF 如何在ASP.NET MVC应用程序中首先使用EF代码检查SQL表中是否存在数据? - How to check if data exist in SQL table using EF code first in ASP.NET MVC application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM