【问题标题】:unable to query child properties in OData with Web Api无法使用 Web Api 查询 OData 中的子属性
【发布时间】:2013-07-08 08:00:28
【问题描述】:

我正在使用 odata 并尝试过滤对子类属性的查询。

我正在使用 MongoDb 存储一系列对象,并希望使用 OData 查询这些对象。对象是从 xml 自动生成的,所以有一些令人讨厌的继承,基本的对象场景如下。

public class Container {
    public Parent Property {get; set;}
}    

public class Parent {}

public class Child : Parent {
    public StringWrapper Value {get; set;}
}

public class StringWrapper {
    public string Value {get; set;}
}

所以我已经将容器变成了一个实体,并制作了一个控制器,其中包含代码:

public ContainerController : ODataController {
    public PageResult<Container> Get(ODataQueryOptions<Container> queryOptions) {
        IQueryable<Container> containers = mongoRepo.All();

        var filteredContainers = queryOptions.ApplyTo(containers)
                                     as IQueryable<Container>;

        return new PageResult<Container>(filteredContainers, 
                                         Request.GetNextPageLink(),
                                         Request.GetInlineCount());
        }
}

然后我用 uri 查询这个:

 http://...Container?$filter=Property/NS.Child/Value/Value eq 'example'

如果我在应用查询选项后抛出一个断点并查看 IQueryable.Expression 它会给出结果:

value(MongoDB.Driver.Linq.MongoQueryable`1[NS.Container])
    .Where($it => (IIF((IIF((($it.Property As Child) == null), null, ($it.Property As Child).Value) == null), null, ($it.Property As Child).Value.Value) == value(System.Web.Http.OData.Query.Expressions.LinqParameterContainer+TypedLinqParameterContainer`1[System.String]).TypedProperty))

当这个问题得到解决后,我收到以下错误

无法确定表达式的序列化信息:。

编辑

我尝试在不使用 mongo db 的情况下实现这个基本案例,并且相同的查询工作正常。然后,我尝试使用 mongo C# 驱动程序进行测试,以将 .where 与 As 方法一起使用,这导致了相同的错误。

我找到了查询

queryable.Where(
it => (it.Property is Child && ((Child)it.Properties).Value.Value == "example"));

工作正常,想知道是否有办法从同一个 uri 将查询转换为这种形式,或者让 mongodb 的 C# 驱动程序将查询转换为这种形式?

【问题讨论】:

  • 你解决过这个问题吗?
  • 不,我们已经编写了自己的模型,因此我们只查询子类。我们会查询http://...Child?$filter=Value/Value eq 'example'

标签: linq mongodb asp.net-web-api odata mongodb-.net-driver


【解决方案1】:

你可以试试这个吗,

var filteredContainers = queryOptions.ApplyTo(containers, 
    new ODataQuerySettings 
    { 
        EnableConstantParameterization = false, 
        HandleNullPropagation = HandleNullPropagationOption.False 
    });

?

【讨论】:

  • 这给了我错误“无法确定表达式的序列化信息:$it.Property.Value.Value 和值的 IQueryable 表达式(MongoDB.Driver.Linq.MongoQueryable` 1[NS.Container]).Where($it => (($it.Property As Child).Value.Value == "example"))
猜你喜欢
  • 1970-01-01
  • 2016-02-15
  • 2012-10-18
  • 2016-08-31
  • 2013-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-23
相关资源
最近更新 更多