LINQ:排序 child object 参数

LINQ: sort child object param

我有 'Executive' objects 有 X 数量的 'ExecutiveSectionMapping' child objects 取决于 Executive 也属于多少部分。

问题是,在查看这些 Executives 时,映射的顺序是随机的。我需要独立于对高管本身进行排序来对 child 的参数进行排序。 试过了没有用:

 return _context.Executives
            .OrderBy(x => x.ExecutiveSectionMappings.OrderBy(y=>y.ExecutiveSectionId))
            .ToList();

也试过这个:

return _context.Executives
            .OrderBy(x => x.ExecutiveSectionMappings.Select((y => y.ExecutiveSectionId)))
            .ToList();

还有这个:

     return _context.Executives
            .Include(x=>x.ExecutiveSectionMappings.OrderBy(y=>y.ExecutiveSectionId))
            .ToList();

不确定如何进行...有什么想法吗?

在你的上下文中好像做不到,但是在内存中可以做到。

List<Executives> executives = _context.Executives.ToList();

executives.ForEach(e => 
    e.ExecutiveSectionMappings = e.ExecutiveSectionMappings.OrderBy(y=>y.ExecutiveSectionId)
   .ToList())