AutoMapper / AutoMapper.Extensions.OData

Creates LINQ expressions from ODataQueryOptions and executes the query.
MIT License
140 stars 38 forks source link

error requesting $count #209

Open mansurdegirmenci opened 4 months ago

mansurdegirmenci commented 4 months ago
   [HttpGet]
   public async Task<IActionResult> Get(ODataQueryOptions<PhotoDto> options)
   {
       var result = await _dataListContext.Photos.GetQueryAsync(_mapper, options);
       return Ok(result);
   }

url: https://localhost:5001/api/Photos/$count

When I make a request to the specified url, it gives the following error, other requests work fine. The version I use is the latest version.

Microsoft.OData.ODataException: The value of type 'Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[[WebUI.DataTransferObject.Server.PhotoDto, WebUI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' could not be converted to a raw string.

BlaiseD commented 4 months ago

May not be supported. Best to add a test so we can see it fail. See examples of supported usage here and here.

HHDanim commented 4 weeks ago

Digging through the sources I found that GetQuery() / GetQueryAsync() sets the OData feature "TotalCount" if you request $count. So this is my solution/workaround I came up with to get $count working:

    public IActionResult Get([FromServices] ODataQueryOptions<Entity> options) {
        IQueryable<Entity> query = dbContext.Entities.GetQuery(mapper, options);
        return options.Count?.Value == true ? Ok(options.Request.ODataFeature().TotalCount) : Ok(query);
    }