I have a .NET 4.6.1 web application that queries Dynamics 365 (F&O) custom entities to retrieve some data. When the result set returned by the query has a single quote in it, the code throws Microsoft.OData.ODataexception: Found an unbalanced bracket expression Exception
I am using the OData v4 Client Code Generator version 7.5.1 to generate the metadata. One of my other applications have a similar configuration and it works there without any issues. I do not know what causes this exception in this particular application.
I tried to manually remove the single quote from the result set that I expect and then it works without any errors. It is common to have single quotes in our data and so I need to make it work with it. Not sure if/how I need to escape the single quotes from the resulting data set.
Notice the single quote in the Name field of the data. Manually removing this single quote from D365 UI allows this result to be returned in the code but if I let it as it is, it gives the error.DataSet Image
Any help is appreciated.
EDIT: Here is the code that is throwing the exception
dynamic locations = (from l in d365.HBSWEBServiceLocations
where (string.IsNullOrEmpty(location) || l.Name.Equals(location + "*")) &&
(string.IsNullOrEmpty(storeNum) || l.StoreNum.Equals(storeNum + "*")) &&
(string.IsNullOrEmpty(address) || l.AddressStreet.Equals(address + "*")) &&
(string.IsNullOrEmpty(city) || l.AddressCity.Equals(city + "*")) &&
(string.IsNullOrEmpty(state) || l.AddressState.Equals(state + "*")) &&
(string.IsNullOrEmpty(zip) || l.AddressZipCode.Equals(zip + "*")) &&
(string.IsNullOrEmpty(country) || l.AddressCountryRegionId.Equals(country + "*")) &&
(l.Status == Els_SMAServiceLocationStatus.New || l.Status == Els_SMAServiceLocationStatus.Active)
&& l.DataAreaId == "hsvc"
orderby l.StoreNum descending, l.Name
select new { l.LocationId, l.Name, l.AddressStreet, l.AddressCity, l.AddressState, l.AddressZipCode, l.AddressCountryRegionId, l.SegmentId, l.StoreNum, l.MarketSegment, l.Chain }).Take(20).ToList();