Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# OrderByDescending按子元素_C#_Asp.net_.net_Entity Framework_Automapper - Fatal编程技术网

C# OrderByDescending按子元素

C# OrderByDescending按子元素,c#,asp.net,.net,entity-framework,automapper,C#,Asp.net,.net,Entity Framework,Automapper,我有两个实体: 博客: 我有两个视图模型: 博客虚拟机: 正如你所看到的,有相同的 我只想获得至少3篇文章的博客,并且只获得3篇最新文章。此外,我想订购降序发布。 对于映射,我使用AutoMapper 我尝试过这样的东西,但是代码并没有像我预期的那样工作 var blogs = _context.Blogs.Include(x => x.Posts) .Where(x.Posts.Count >= 4) .Project

我有两个实体: 博客:

我有两个视图模型: 博客虚拟机:

正如你所看到的,有相同的

我只想获得至少3篇文章的博客,并且只获得3篇最新文章。此外,我想订购降序发布。

对于映射,我使用AutoMapper

我尝试过这样的东西,但是代码并没有像我预期的那样工作

var blogs = _context.Blogs.Include(x => x.Posts)
                .Where(x.Posts.Count >= 4)
                .ProjectTo<BlogVm>()
                .OrderByDescending(x => x.Posts.OrderByDescending(y => y.Published)).ToList();
var blogs=\u context.blogs.Include(x=>x.Posts)
。其中(x.Posts.Count>=4)
.ProjectTo()
.OrderByDescending(x=>x.Posts.OrderByDescending(y=>y.Published)).ToList();
我收到一条错误消息:

“System.ArgumentException:'必须至少实现一个对象 我可比。”


现在你要求它按每个博客的已排序帖子来排序
博客
s,这。。。没有道理。您可以按最近的发布日期对
博客
进行排序,并分别按发布日期对每个
博客
进行排序

var blogs = _context.Blogs.Include(x => x.Posts)
                .Where(x.Posts.Count >= 4)
                .ProjectTo<BlogVm>()
                .OrderByDescending(x => x.Posts.Max(y => y.Published)).ToList();

foreach(var blog in blogs) {
    blog.Posts.Sort((x,y) => y.Published.CompareTo(x.Published));
}
var blogs=\u context.blogs.Include(x=>x.Posts)
。其中(x.Posts.Count>=4)
.ProjectTo()
.OrderByDescending(x=>x.Posts.Max(y=>y.Published)).ToList();
foreach(博客中的var博客){
blog.Posts.Sort((x,y)=>y.Published.CompareTo(x.Published));
}

您可以这样尝试

var blogs = _context.Blogs.Include(x => x.Posts)
    .Where(x.Posts.Count >= 4)
    .ProjectTo<BlogVm>()
    .Select(x => new
    {
        Blog = x,
        //Take newly published 3 posts for per Blog
        RecentlyPosts = x.Posts.OrderByDescending(y => y.Published).Take(3)
    });
var blogs=\u context.blogs.Include(x=>x.Posts)
。其中(x.Posts.Count>=4)
.ProjectTo()
.选择(x=>new
{
Blog=x,
//为每个博客发表3篇新文章
RecentlyPosts=x.Posts.OrderByDescending(y=>y.Published).Take(3)
});

另外,最好创建一个模型类来选择已知类型而不是匿名类型。

为什么不选择一些
quick
blogs.ForEach(x=>x.Posts.Sort((y,z)=>z.Date.CompareTo(y.Date))@mutk;现在:这在客观上是一种改进吗?在我看来,
ForEach
方法被过度使用了。
public int Id { get; set; }
public string Name { get; set; }
public DateTime Published { get; set; }
var blogs = _context.Blogs.Include(x => x.Posts)
                .Where(x.Posts.Count >= 4)
                .ProjectTo<BlogVm>()
                .OrderByDescending(x => x.Posts.OrderByDescending(y => y.Published)).ToList();
var blogs = _context.Blogs.Include(x => x.Posts)
                .Where(x.Posts.Count >= 4)
                .ProjectTo<BlogVm>()
                .OrderByDescending(x => x.Posts.Max(y => y.Published)).ToList();

foreach(var blog in blogs) {
    blog.Posts.Sort((x,y) => y.Published.CompareTo(x.Published));
}
var blogs = _context.Blogs.Include(x => x.Posts)
    .Where(x.Posts.Count >= 4)
    .ProjectTo<BlogVm>()
    .Select(x => new
    {
        Blog = x,
        //Take newly published 3 posts for per Blog
        RecentlyPosts = x.Posts.OrderByDescending(y => y.Published).Take(3)
    });