Base solution for your next web application
Open Closed

How do you sort child entities returned by the GetAllIncluding method? #9590


User avatar
0
tom.ohle created

We are using the ASP.NET Zero template version 9.0.1. How do you go about sorting child entites when they are retrieved using the GetAllIncluding method? According to the Microsoft documentation we should be able to do the following. https://docs.microsoft.com/en-us/ef/core/querying/related-data#filtered-include.

return await _entityRepository.GetAllIncluding( e => e.childEntities .OrderBy( ce => ce.childProperty) ) .ToListAsync();


2 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    hi

    You can order the children entities after query.

    https://entityframeworkcore.com/knowledge-base/54859088/orderby-in-include-child-using-ef-core

  • User Avatar
    0
    tom.ohle created

    That worked for us. Our method now looks similar to the following. Thank you.

    public async Task<List<ParentItem>> GetParentItems() var parentItems = _parentItemRepository.GetAllIncluding( p => p.ChildItem1, p => p.ChildItem2 ) .AsNoTracking() .ToListAsync(); parentItems.Result.ForEach( p => { p.ChildItem1 = p.ChildItem1.OrderBy(c => c.ChildProperty).ToList(); p.ChildItem2 = p.ChildItem2.OrderBy(c => c.ChildProperty).ToList(); }); return await parentItems; }