I've inherited some code and does stuff with OData and I've spent some time on it to no avail so now I need some help please
I have a database that has a table called Client and another with ClientContacts
What I need to be able to do is using OData, get a list of clients what have a contact first name that contains ‘Chris’
This is what I tried to start with but obviously this won’t work as client contracts are a collection
localhost:55992/api/ClientApi/GetAllClients?$expand=ClientContacts,Client&$filter=substringof('',ClientName) and substringof('Chris',ClientContacts/ContactFirstName)&$top=20&$skip=0&$inlinecount=allpages
It gives me the following exception
"The parent value for a property access of a property 'ContactFirstName' is not a single value. Property access can only be applied to a single value."
I think I need to use the Any command but I can’t for the life of me figure out, any help will be much appreciated
The syntax for using a filter with the any operator looks like this:
$filter=ClientContacts/any(contact: substringof('Chris',contact/ContactFirstName))
Related
I am currently trying to make an Odata call where I filter by a "multi-level" expanded property, however, none of the documentation or information I can find on this covers exactly what I am trying to accomplish. Does anyone know if this can be done, and how?
This is for letting our users be able to filter their Tests by the name of the Room the test was taken in. We have been able to filter by a single level of expanding, letting users filter their Tests by the name of the location, but we just can't seem to get the syntax right with the second level.
For context, our Test table is linked to a location table that is linked to a room table.
We are able to see the expanded information without a filter by calling:
.../api/Tests$expand=Location($select=Id,LocationName;$expand=Room($select=Id,RoomName))
returns:
...{"Id":19955,"LocationId":102,"Location":{"Id":102,"LocationName":"TestLocation","Room":{"Id":8,"RoomName":"TestRoom"}},...
But when we try a filtered call such as:
.../api/Tests$expand=Location($select=Id,LocationName;$expand=Room($select=Id,RoomName))&$filter=contains(Location/Room/RoomName,%27a%27))
All we get is the standard:
{"error":{"code":"NotFound","message":"The type 'Edm.String' is not an entity type. Only entity types support $select and $expand.\r\nParameter name: context"}}
There is surely some way of being able to filter from a multi-levelled expand, but I am somewhat of a newbie to Using OData, so any help would be appreciated!
Also, I believe we are using OData - 7.01.
Just answering my own question here, as it turns out the way we were doing it was correct.
The system was breaking in other areas down the pipe.
I have a problem where I need to select a specific value from a specific entity from a entity set, however, I need to do it in a way without knowing the key.
This is the query I actually need:
odata/..../picklistLabels(locale='en_GB',optionId=10819)/label
However I need to program it in a way so it automatically selects the label without knowing the optionId. Is there a way to do this in OData?
From your question, I think that you want to perform a navigation but you don't have a key. Unfortunately, this exact functionality isn't available, however, you can perform an expand like this:
odata/..../picklistLabels?$filter=locale eq 'en_GB' and optionId=10819&$expand=label
This will get you the same information that the other call would do but in a slightly different format, you would have to find the first element in the array and then get the label property to get that information
As an aside, if you have the option to change the server (I'm guessing not due to the sapui5 tag but it might be useful for other users) you could change the key. If the locale and the optionId are enough to identify the object, then you could make these into a composite key. Here is a link for an example within the WebAPI OData source code: https://github.com/OData/ODataSamples/tree/master/WebApi/v4/ODataCompositeKeySample
I am using an OData model in my SAP UI5 application. I do the following:
var oView = this.getView();
this._oModel = oView.getModel();
which I thought loaded the model and allowed me to access whatever I needed to inside the model. However whenever I try to get a property using getProperty(path) it returns undefined. I know the path is correct because I used it elsewhere in my application and it worked okay. The model I am using is named "metadata.xml" if that helps.
If you read the documentation provided by SAP talking about OData services (I assume v2), you might find your answer there. Since you are using getProperty(), "you can only access single entities and properties with these methods. To access entity sets, you can get the binding contexts of all read entities via a list binding." Therefore you may want to a read() of the entity set first and then use the results of this read to access whatever property you want.
Link to doc: https://sapui5.hana.ondemand.com/#docs/guide/6c47b2b39db9404582994070ec3d57a2.html
To provide some context, I'm using jqGrid to conduct a server side search on a datetime field, truncating the time portion server side, and then fetching results via a stored procedure. This is being done using System.Dynamic.Linq, which I've modified slightly as per this question here. I'm using EF6 Code First approach. The relevant portion of the code looks something like this:
//For matching date instead of datetime values
if (wc.Clause.Contains("Date"))
{
wc.Clause = wc.Clause.Replace("DeliveryDate", "DbFunctions.TruncateTime(DeliveryDate)");
}
results = dataFileDB.Database.SqlQuery<Document>("exec [dbo].[sp_getDocumentDetails]").AsQueryable().Where(wc.Clause, wc.FormatObjects);
The problem I'm having is that because the query is fetched using a stored procedure, I get an exception on the last line saying
System.NotSupportedException: This function can only be invoked from LINQ to Entities.
at System.Data.Entity.DbFunctions.TruncateTime(Nullable`1 dateValue)
In other places where I've used a lambda query to fetch directly from the entity itself and apply the where clause filter, the filtering is done correctly.
What I was trying to do (and I'm not sure if this is possible but...) was run an entity query against the results variable and apply the where clause to that variable but I've had no luck. Is this the right way to go or is there another approach I could take?
Thanks in advance!
Got it... I was overcomplicating things too much.
I just converted the DateTime field to a Date field when returning it via the stored procedure. Doing so no longer required me to use the DbFunctions.TruncateTime method, thereby resolving my issue.
Hope this helps someone else!
This error message is driving me nuts.
I'm getting it when using Html.ListBox and Html.DropDownList HtmlHelpers with ASP.NET MVC v1.0. Populating the lists works OK - I can view them, etc - but when I go to create a new record in the Model using the FormCollection passed into the Controller to get the selected value, this error occurs. I've tried several different configurations and code arrangements as per several other posts on this matter - no joy.
Hopefully someone can tell me whether this is a known issue or not so as I can move on with my life! Would prefer not to have to post code if necessary, but can do if requested.
Oh, and a suggested workaround/solution would be nice too ;-)
TIA!
Bernard.
Ok, quick update, I've posted three comments to responses below clearing up details as to what I'm up to here.
I now have another issue to share - I've tried ScottGu's alternative recommended approach - implementing a ModelView. So, when I do this it's basically the same scenario, but I get an "Object reference not set to an instance of an object." error at the same place that I was getting "There is no ViewData item with the key 'Blah' of type 'IEnumerable'.".
I'm starting to thing that this is perhaps something to do with the way the EF works...
Ok, sooo I'm clearly an EF/MVC noob...
Although the issue was solved by my actually implementing proper exception management...the exception message (when I actually started catching it) was "The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects.". So I'm using the same ObjectContext for all data access now, and things are back on track.
Thanks for caring, I'll have a smarter question next time - promise!
Bernard.
Not knowing how you are coding this will be hard for people to identify the problematic area. Have a read of this forum thread - further down has a similar error to you.
http://forums.asp.net/p/1320426/2623986.aspx
You can again bind your select list in the according post method where validation occurs.
For example you have an error inside the create view of the controller than you can again bind dropdown list in create post method.