5

I need to do a request on an OData service that would retrieve a single entity and that should look like this: /EntitySet(par1=value1,…,parn=valuen)

However, my LINQ generated query for fetching the entry looks like this: /EntitySet()?$filter=(par1 eq value1) and (par2 eq value2) and ... (parn eq valuen)

This is, of course, valid. But the server does not allow it. It only agrees with the first option, the one with the criteria in parentheses.

Is there something I can do? It would be a shame to manually create the query URL...

Here's the code:

        var context = new CHART_SRV_Entities(oDataUri);

        var query = context.ApplicationData.Where(ad =>
            ad.institution == "1" &&
            ad.patientId == "2000118" &&
            ad.caseId == "2488");

        DataServiceCollection<ApplicationData> data = new DataServiceCollection<ApplicationData>(context);

        data.LoadCompleted += (s, args) =>
            {
                if (args.Error == null)
                {
                    if (data.Continuation != null)
                    {
                        data.LoadNextPartialSetAsync();
                    }
                    else
                    {
                        var result = data;
                    }
                }
                else
                {
                    MessageBox.Show(args.Error.Message);
                }
            };

        data.LoadAsync(query);

Update: A "parentheses request" was achieved with CreateQuery here. However, that's a synchronous call. Any ideas on how to make it async?

Update2: Seems that what I'm trying to achieve is an OData composite key lookup. There's this OData library that can do it, but I find it hard to believe that this can't be done with the Microsoft toolset.

Community
  • 1
  • 1
anomistu
  • 61
  • 4
  • Try removing the parentheses after entity set "~/EntitySet?$filter=(par1 eq value1) and (par2 eq value2) and ... (parn eq valuen)". What is your service built with? – Feng Zhao Jul 02 '14 at 03:28
  • Removing the parentheses returned same response from server: Method 'APPLICATIONDATA_GET_ENTITYSET' not implemented in data provider class It is an SAP Gateway service btw – anomistu Jul 02 '14 at 07:33

1 Answers1

2

Only key properties are allowed to appear in the parentheses after the entity set name. If you are using non-key property to find the entry, you have to use the filter. This is how client works, and it is correct behavior.

In your case, I would suggest either customize client code or update the service code to support the filter, if you can.

Bin_MSFT
  • 153
  • 3
  • The entity was created automatically when I added the service reference. The generated code looks like this: [global::System.Data.Services.Common.EntitySetAttribute("ApplicationData")] [global::System.Data.Services.Common.DataServiceKeyAttribute("institution", "refresh", "wdConfigId", "defaultLayoutId", "patientId", "caseId", "dateFrom", "dateTo", "layoutId", "guid", "documentOu", "movementId")] public partial class ApplicationData : global::System.ComponentModel.INotifyPropertyChanged I'm not sure what I can change... – anomistu Jul 02 '14 at 07:38
  • 1
    There are multiple keys, but you are only specifying three of them through where clause. That means you are not to finding a single entity, but a collection of entity. So the client code HAVE TO use the filter to do what you told it to do. If you want to return a single entity, you have to specify all the key properties in the where. – Bin_MSFT Jul 03 '14 at 02:13