3

I have a web services API (with an OData endpoint called piperuns, using ODataController) that takes an optional query string (called projectNumber) like:

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

I have a client based on Simple.OData.Client, and I cannot figure out how to pass this optional query string...I am using the dynamic syntax and can get the piperuns (without the query parameter) using the syntax below:

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

But I have not found any information on how to include my optional query string parameter, if desired?

Thanks!

Nelson Bond
  • 31
  • 1
  • 4

2 Answers2

2

For conditions that include metadata model properties you should use Filter clause:

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

However, if the extra clause is not related to the model, I'd use Filter overload that takes a string:

IEnumerable pipeRunsNext = await client
  .For(x.piperuns)
  .Filter("projectNumber == '1'")
  .FindEntriesAsync(annotations.NextPageLink, annotations);
Vagif Abilov
  • 9,835
  • 8
  • 55
  • 100
  • I get the following error: "Invalid referenced object projectNumber " projectNumber is a simple query string (optional) and is received like: [EnableQuery(PageSize = 10)] [ODataRoute] public IQueryable Get(string projectNumber = "1") so it is not part of the data model at all – Nelson Bond Mar 07 '15 at 15:50
  • I see. I overlooked that projectNumber is not the part of the model. You may try then using a Filter clause with a string parameter, e.g. For(x.piperuns).Filter("projectNumber eq 1"). I think it should work, let me know if it does not. – Vagif Abilov Mar 08 '15 at 07:03
  • no luck, throws a -2146233088, System.AggregateException, inner exception is "Bad Request" from Simple.OData.Client.WebRequestException. – Nelson Bond Mar 09 '15 at 12:09
  • Let me investigate then. I will get back to you. – Vagif Abilov Mar 09 '15 at 13:59
  • Hmm, I wrote a small test and it worked. Did you remember to enclause "1" in equality expression in single quotes? If it still fails, can you open an issue at https://github.com/object/Simple.OData.Client? – Vagif Abilov Mar 09 '15 at 18:33
1

Now you can make use of QueryOptions to pass custom query parameters.

e.g.

IEnumerable pipeRunsNext = await client
  .For(x.piperuns)
  .QueryOptions("projectNumber=1")
  .FindEntriesAsync(annotations.NextPageLink, annotations);
H77
  • 5,859
  • 2
  • 26
  • 39