type system.linq.enumoblequery的对象无法转换为type system.collections.generic.ienumerab

发布于 2025-01-30 00:43:12 字数 1231 浏览 2 评论 0原文

我遇到了一个问题,可以在Web API上检索带有ODATA查询的XML数据类型,但是以JSON格式毫无问题。

错误这样说:

参数exception:类型'system.linq.enumoblequery 1 [microsoft.aspnetcore.odata.query.query.wrapper.selectsome 1 [odata_webapi.weatherforecast]] collections.generic.ienumerable`1 [odata_webapi.weatherforecast]'。

我只是在示例控制器 WeatherForecastController.cs 文件创建新的Web API项目时对其进行测试。

    [HttpGet("GetWeatherForecast/{format}"), FormatFilter]
    [EnableQuery]
    public IEnumerable<WeatherForecast> Get()
    {
        return Enumerable.Range(1, 5).Select(index => new WeatherForecast
        {
            Date = DateTime.Now.AddDays(index),
            TemperatureC = Random.Shared.Next(-20, 55),
            Summary = Summaries[Random.Shared.Next(Summaries.Length)]
        })
        .ToArray();
    }

在没有ODATA查询的情况下检索XML数据类型效果很好,但是我希望它可以与ODATA查询一起使用,该怎么做?希望任何人都可以帮助我。

这起作用:

https://localhost:7110/WeatherForecast/GetWeatherForecast/xml

但不是这个

https://localhost:7110/WeatherForecast/GetWeatherForecast/xml?$select=Summary

I'm running into a problem retrieving XML datatype with OData Query on the Web API, but in JSON format there is no problem at all.

Error saying this :

ArgumentException: Object of type 'System.Linq.EnumerableQuery1[Microsoft.AspNetCore.OData.Query.Wrapper.SelectSome1[OData_WebAPI.WeatherForecast]]' cannot be converted to type 'System.Collections.Generic.IEnumerable`1[OData_WebAPI.WeatherForecast]'.

I'm just testing it on the sample Controller WeatherForecastController.cs file when you create a new Web API project.

    [HttpGet("GetWeatherForecast/{format}"), FormatFilter]
    [EnableQuery]
    public IEnumerable<WeatherForecast> Get()
    {
        return Enumerable.Range(1, 5).Select(index => new WeatherForecast
        {
            Date = DateTime.Now.AddDays(index),
            TemperatureC = Random.Shared.Next(-20, 55),
            Summary = Summaries[Random.Shared.Next(Summaries.Length)]
        })
        .ToArray();
    }

Retrieving XML datatype without OData Query works fine, but I want it to work with OData Query, How to do that? hoping anyone could help me.

This works :

https://localhost:7110/WeatherForecast/GetWeatherForecast/xml

But not this

https://localhost:7110/WeatherForecast/GetWeatherForecast/xml?$select=Summary

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文