使用ODataQueryOptions时无法应用$ select [英] Can't apply $select when using ODataQueryOptions

查看:51
本文介绍了使用ODataQueryOptions时无法应用$ select的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过EF Core和OData(7.1.0)实现对Sql Server的查询.

Action方法如下:

 <代码> [HttpGet]公共IEnumerable< UserInfoDto>Get(ODataQueryOptions ops){返回this.service.GetUserInfos(ops);} 

服务代码:

 公共列表< UserInfoDto>GetUserInfos(ODataQueryOptions ops){使用(var context = new EFContext()){var query = context.Users.Join(context.Customers,x => x.CustomerId,y => y.Id,(x,y)=> new UserInfoDto{ID = x.Id,名称= x.Name,年龄= x.年龄,CustomerId = x.CustomerId,CustomerTitle = y.Title,客户描述= y.描述});var result = ops.ApplyTo(query).Cast< UserInfoDto>().ToList();返回结果;}} 

启动配置方法:

 公共无效配置(IApplicationBuilder应用程序,IHostingEnvironment env){app.UseMvc(b =>{b.Count().Filter().OrderBy().Select().MaxTop(null);b.EnableDependencyInjection();});} 

但是,当我在查询中使用 $ select 时(例如 https://localhost:5001/api/userinfos?$ select = id ),而不是预期的结果,我得到一个错误:

InvalidOperationException:类型之间未定义强制运算符'Microsoft.AspNet.OData.Query.Expressions.SelectExpandBinder + SelectSome`1 [OdataApp.UserInfoDto]"和"OdataApp.UserInfoDto".



我想念什么?感谢您的帮助.

解决方案

在对OData使用 $ select 查询选项时,需要使用 dynamic 的IQueryable.>

例如,想象下面的类:

 公共类Person {public int ID {get;放 }公共字符串名称{get;放;}} 

因此,如果您的查询中没有 $ select ,则IQueryable将生成一个Person的集合,在每个项目中都有所有属性(id和name).

但是,如果使用 $ select = id 查询,则每个项目将仅具有ID属性,并且无法将动态类型转换为Person类型.

换句话说,您不能使用 $ select 并返回 List< UserInfoDto> ,而您需要返回 List< dynamic> 没有Cast方法,就像这样:

  var result = ops.ApplyTo(query)as IQueryable< dynamic> ;;返回result.ToList(); 

I am trying to implement querying to Sql Server via EF Core and OData(7.1.0).

Action method looks like follows:

[HttpGet]
public IEnumerable<UserInfoDto> Get(ODataQueryOptions ops)
{
     return this.service.GetUserInfos(ops);
}

Service code:

public List<UserInfoDto> GetUserInfos(ODataQueryOptions ops)
{
    using (var context = new EFContext())
    {
        var query = context.Users.Join(context.Customers, x => x.CustomerId, y => y.Id, (x, y) => new UserInfoDto
        {
            Id = x.Id,
            Name = x.Name,
            Age = x.Age,
            CustomerId = x.CustomerId,
            CustomerTitle = y.Title,
            CustomerDescription = y.Description
        });

        var result = ops.ApplyTo(query).Cast<UserInfoDto>().ToList();
        return result;
    }
}

Startup Configute method:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
     app.UseMvc(b => 
     {
         b.Count().Filter().OrderBy().Select().MaxTop(null);
         b.EnableDependencyInjection();
     });
}

However, when I am having $select in query (e.g https://localhost:5001/api/userinfos?$select=id), instead of projected result I am getting an error:

InvalidOperationException: No coercion operator is defined between types 'Microsoft.AspNet.OData.Query.Expressions.SelectExpandBinder+SelectSome`1[Oda taApp.UserInfoDto]' and 'OdataApp.UserInfoDto'.



What am I missing? Any help is appreciated.

解决方案

When you using $select query option for OData, you need an IQueryable of dynamic .

For example, imagine the following class:

public class Person {
    public int Id { get; set }
    public string Name { get; set; }
}

So, if you have a query without $select, the IQueryable will generate a collection of Person, with all properties (id and name) in each item.

But, if you use a $select=id query, each item will have only the ID property and you cant Cast a dynamic type to a Person type.

In other words, you cant use $select and return a List<UserInfoDto>, you need to return a List<dynamic> without the Cast method, just like this:

    var result = ops.ApplyTo(query) as IQueryable<dynamic>;
    return result.ToList();

这篇关于使用ODataQueryOptions时无法应用$ select的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆