[英]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: 但我有两个问题:
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活动
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.