ToDataSourceResult applied to IQueryable with included navigation properties

2 posts, 0 answers
  1. Mauro
    Mauro avatar
    2 posts
    Member since:
    Dec 2013

    Posted 28 Apr 2020 Link to this post

    Hello, I'm currently devloping a .NETCore application. The application is divided into three layers: Data Layer(with models definition), Service Layer (to retrieve data and return it as IQueryable objects) and the Application Layer (where Telerik is installed).

    My controller read action in the ApplicationLayer is as follows:

     public IActionResult ReadAziende([FromServices] IAziendaService service, [DataSourceRequest] DataSourceRequest request)
            {
                  var aziende = service.ListWithIncludes().ToDataSourceResult(request, m => _mapper.Map<IndiceAziendaVM>(m));
                return Json(aziende);
            }

     

    The Service Layer action ListWithIncludes is as follows:

      public IQueryable<AziendaFullIncludeDTO> ListWithIncludes()
            {
                return _context.Aziende
                    .Include(a => a.Settore)
                    .Include(a => a.Indirizzi)
                    .Include(a => a.Tags)
                        .ThenInclude(a => a.Tag)
                    .Include(a => a.CodiceAteco)
                    .Include(a => a.TenantDataAccess)
                        .ThenInclude(a => a.Tenant)
                    .AsNoTracking()               
                    .Select(a => _mapper.Map<AziendaFullIncludeDTO>(a))   ;
            }

     

    The Service Layer is correctly returning the DTO with included fields in an IQueryable Object but as soon as I pass it to ToDataSourceResult all included fields get lost.
    What is the correct way to apply ToDataSourceResult  to an IQueryable Object with Included navigation properties?
    Thanks.

     

  2. Plamen
    Admin
    Plamen avatar
    3079 posts

    Posted 30 Apr 2020 Link to this post

    Hello,

    The issue is most probably observed because ToDataSourceResult extension method applies sorting automatically based on the parameters passed by the Grid. I assume this overwrites your sort expression. For more information you can refer to this forum thread where a similar scenario have been discussed.

    Regards,
    Plamen
    Progress Telerik

    Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
    Our thoughts here at Progress are with those affected by the outbreak.
Back to Top