Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error requesting $count #209

Open
mansurdegirmenci opened this issue Mar 2, 2024 · 2 comments
Open

error requesting $count #209

mansurdegirmenci opened this issue Mar 2, 2024 · 2 comments

Comments

@mansurdegirmenci
Copy link

   [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
Copy link
Member

BlaiseD commented Mar 8, 2024

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

@HHDanim
Copy link

HHDanim commented Jun 28, 2024

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);
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants