【问题标题】:simple.odata.client query parametersimple.odata.client 查询参数
【发布时间】:2015-02-24 13:22:10
【问题描述】:

我有一个 Web 服务 API(带有一个称为 piperuns 的 OData 端点,使用 ODataController),它接受一个可选的查询字符串(称为 projectNumber),例如:

http://localhost:59636/piperuns?projectNumber=1

我有一个基于 Simple.OData.Client 的客户端,但我无法弄清楚如何传递这个可选的查询字符串...我使用的是动态语法,并且可以使用该语法获取管道运行(不带查询参数)下面:

ODataFeedAnnotations annotations = new ODataFeedAnnotations();
ODataClient client = new ODataClient("http://localhost:59636/");

var x = ODataDynamic.Expression;
IEnumerable<dynamic> pipeRunsNext = await(Task<IEnumerable<Simple.OData.Client.ODataEntry>>)client
                .For(x.piperuns)
                .FindEntriesAsync(annotations.NextPageLink, annotations);

但我没有找到任何关于如何包含可选查询字符串参数的信息(如果需要)?

谢谢!

【问题讨论】:

    标签: odata simple.odata


    【解决方案1】:

    对于包含元数据模型属性的条件,您应该使用 Filter 子句:

    IEnumerable pipeRunsNext = await client
      .For(x.piperuns)
      .Filter(x.projectNumber == "1")
      .FindEntriesAsync(annotations.NextPageLink, annotations);
    

    但是,如果额外的子句与模型无关,我会使用带有字符串的过滤器重载:

    IEnumerable pipeRunsNext = await client
      .For(x.piperuns)
      .Filter("projectNumber == '1'")
      .FindEntriesAsync(annotations.NextPageLink, annotations);
    

    【讨论】:

    • 我收到以下错误:“Invalid referenced object projectNumber” projectNumber 是一个简单的查询字符串(可选),接收方式如下:[EnableQuery(PageSize = 10)] [ODataRoute] public IQueryable Get(string projectNumber = "1") 所以它根本不是数据模型的一部分
    • 我明白了。我忽略了 projectNumber 不是模型的一部分。然后,您可以尝试使用带有字符串参数的 Filter 子句,例如For(x.piperuns).Filter("projectNumber eq 1")。我认为它应该有效,如果无效,请告诉我。
    • 不走运,抛出 -2146233088,System.AggregateException,内部异常是来自 Simple.OData.Client.WebRequestException 的“错误请求”。
    • 那我来调查一下。我会回复你的。
    • 嗯,我写了一个小测试,它成功了。你还记得在等式表达式中用单引号括起来“1”吗?如果仍然失败,您可以在github.com/object/Simple.OData.Client 上打开一个问题吗?
    【解决方案2】:

    现在您可以使用QueryOptions 传递自定义查询参数。

    例如

    IEnumerable pipeRunsNext = await client
      .For(x.piperuns)
      .QueryOptions("projectNumber=1")
      .FindEntriesAsync(annotations.NextPageLink, annotations);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-15
      • 1970-01-01
      • 1970-01-01
      • 2017-08-31
      • 2018-03-17
      • 2017-02-05
      • 2013-07-01
      相关资源
      最近更新 更多