Asp.net 在主页上选择网站的最后4篇文章

Asp.net 在主页上选择网站的最后4篇文章,asp.net,entity-framework,asp.net-core,Asp.net,Entity Framework,Asp.net Core,我想在主页上以简洁的方式显示最后一个站点的最后4篇文章。我已经做了一个模型视图,我只有一个ef核心的问题 视图模型: public class ShowBlogForHomePageViewModel { public int BlogId { get; set; } public string BlogTitle { get; set; } public string BlogUrl { get; set; } publi

我想在主页上以简洁的方式显示最后一个站点的最后4篇文章。我已经做了一个模型视图,我只有一个ef核心的问题

视图模型:

public class ShowBlogForHomePageViewModel
    {
        public int BlogId { get; set; }
        public string BlogTitle { get; set; }
        public string BlogUrl { get; set; }
        public string BlogImageName { get; set; }
        public string MetaDescriptionBlog { get; set; }
    }
IBlogService:

List<ShowBlogForHomePageViewModel> GetBlogPostForHome();
  public List<ShowBlogForHomePageViewModel> GetBlogPostForHome()
        {
            var ListBlogs= _context.Blogs.OrderByDescending(b => new ShowBlogForHomePageViewModel()
            {
                BlogId = b.BlogId,
                BlogImageName = b.BlogImageName,
                BlogTitle = b.BlogTitle,
                MetaDescriptionBlog = b.MetaDescriptionBlog,
                BlogUrl = b.BlogUrl 
            }).Take(4).ToList();
            return null;
        }

List GetBlogPostForHome();
博客服务:

List<ShowBlogForHomePageViewModel> GetBlogPostForHome();
  public List<ShowBlogForHomePageViewModel> GetBlogPostForHome()
        {
            var ListBlogs= _context.Blogs.OrderByDescending(b => new ShowBlogForHomePageViewModel()
            {
                BlogId = b.BlogId,
                BlogImageName = b.BlogImageName,
                BlogTitle = b.BlogTitle,
                MetaDescriptionBlog = b.MetaDescriptionBlog,
                BlogUrl = b.BlogUrl 
            }).Take(4).ToList();
            return null;
        }

公共列表GetBlogPostForHome()
{
var ListBlogs=_context.Blogs.OrderByDescending(b=>new ShowBlogForHomePageViewModel()
{
BlogId=b.BlogId,
BlogImageName=b.BlogImageName,
BlogTitle=b.BlogTitle,
MetaDescriptionBlog=b.MetaDescriptionBlog,
BlogUrl=b.BlogUrl
}).采取(4)措施;
返回null;
}
我认为这部分是真的。请核对:

public List<ShowBlogForHomePageViewModel> GetBlogPostForHome()
        {
            return _context.Blogs.OrderBy(b=>b.BlogId).Select(b => new ShowBlogForHomePageViewModel()
            {
                BlogId = b.BlogId,
                BlogImageName = b.BlogImageName,
                BlogTitle = b.BlogTitle,
                MetaDescriptionBlog = b.MetaDescriptionBlog,
                BlogUrl = b.BlogTitle
            }).TakeLast(4).ToList();
        }
公共列表GetBlogPostForHome()
{
return _context.Blogs.OrderBy(b=>b.BlogId)。选择(b=>newshowBlogforHomePageViewModel()
{
BlogId=b.BlogId,
BlogImageName=b.BlogImageName,
BlogTitle=b.BlogTitle,
MetaDescriptionBlog=b.MetaDescriptionBlog,
BlogUrl=b.BlogTitle
}).TakeLast(4.ToList();
}
试试这个

public List<ShowBlogForHomePageViewModel> GetBlogPostForHome()
        {
            return  _context.Blogs
            .OrderByDescending(b => b.BlogId)
           .Select(b => new ShowBlogForHomePageViewModel
            {
                BlogId = b.BlogId,
                BlogImageName = b.BlogImageName,
                BlogTitle = b.BlogTitle,
                MetaDescriptionBlog = b.MetaDescriptionBlog,
                BlogUrl = b.BlogUrl 
            }).Take(4)
              .ToList();
            
        }
公共列表GetBlogPostForHome()
{
return\u context.Blogs
.OrderByDescending(b=>b.BlogId)
.选择(b=>new ShowBlogForHomePageViewModel
{
BlogId=b.BlogId,
BlogImageName=b.BlogImageName,
BlogTitle=b.BlogTitle,
MetaDescriptionBlog=b.MetaDescriptionBlog,
BlogUrl=b.BlogUrl
}).Take(4)
.ToList();
}

谢谢,谢尔盖。帖子更新。请检查。我认为这是真的,你说你有问题,但你没有描述问题是什么。代码做错了什么?你有错误吗?它与你的愿望有什么不同?