23

I am getting the following exception when calling OData from my Kendo ListView:

"A binary operator with incompatible types was detected. Found operand types 'Edm.Guid' and 'Edm.String' for operator kind 'Equal'"

DECODED FILTER:
$filter=OrganizationId eq '4c2c1c1e-1838-42ca-b730-399816de85f8'

ENCODED FILTER:
%24filter=OrganizationId+eq+%274c2c1c1e-1838-42ca-b730-399816de85f8%27

HAVE ALSO UNSUCESSFULLY TRIED THESE FILTERS:
$filter=OrganizationId eq guid'4c2c1c1e-1838-42ca-b730-399816de85f8'
$filter=OrganizationId eq cast('4c2c1c1e-1838-42ca-b730-399816de85f8', Edm.Guid)

MY WEB API CALL LOOKS LIKE:

// GET: odata/Sites
[HttpGet]
[EnableQuery]
public IHttpActionResult GetSites(ODataQueryOptions<Site> queryOptions)
{
    IQueryable<Site> sites = null;

    try
    {
        queryOptions.Validate(_validationSettings);
        sites = _siteService.GetAll().OrderBy(x => x.SiteName);

        if (sites == null)
            return NotFound();
    }
    catch (ODataException ex)
    {
        TraceHandler.TraceError(ex);
        return BadRequest(ex.Message);
    }

    return Ok(sites);
}

MY JAVASCRIPT KENDO DATASOURCE LOOKS LIKE:

var dataSource = new kendo.data.DataSource({

    filter: { field: "OrganizationId", operator: "eq", value: that.settings.current.customer.id },
    schema: {
        data: function (data) {
            return data.value;
        },
        total: function (data) {
            return data.length;
        }
    },
    serverFiltering: true,
    serverPaging: true,
    transport: {
        parameterMap: function (options, type) {

            var paramMap = kendo.data.transports.odata.parameterMap(options);

            // Remove invalid Parameters that Web API doesn't support
            delete paramMap.$inlinecount; // <-- remove inlinecount
            delete paramMap.$format; // <-- remove format
            delete paramMap.$callback; // <-- remove callback

            // PLEASE NOTICE: That I have tried reformatting unsuccessfully
            //paramMap.$filter = paramMap.$filter.replace("OrganizationId eq ", "OrganizationId eq guid");
            //paramMap.$filter = "OrganizationId eq cast('81de6144-987c-4b6f-a9bd-355cb6597fc1', Edm.Guid)";

            return paramMap;
        },
        read: {
            url: buildRoute('odata/Sites')
            , dataType: 'json'
        }
    },
    type: 'odata'
});
Prisoner ZERO
  • 13,848
  • 21
  • 92
  • 137
  • 1
    I've seen [multiple](https://aspnetwebstack.codeplex.com/workitem/420) [resources](http://stackoverflow.com/questions/3890034/problems-with-an-odata-filter-and-a-guid-field) stating that using the guid'[VALUE]' syntax is correct, could you double check that? – Sven Grosen Mar 11 '15 at 19:46
  • I saw those and tried it in this attempt (noted above): $filter=OrganizationId eq guid'4c2c1c1e-1838-42ca-b730-399816de85f8' Do you think this is (somehow) formatted incorrectly? I am stuck. – Prisoner ZERO Mar 11 '15 at 19:50
  • Can you call it successfully via fiddler or directly in the browser instead of via Kendo? – Sven Grosen Mar 11 '15 at 20:26
  • Nope...directly entering the URL is how I was able to view the message (above) – Prisoner ZERO Mar 11 '15 at 20:37
  • What version of OData are you using? The guid'...' syntax was only required for older versions of OData, when we standardized we simplified this to be just '...' as in your "DECODED FILTER". Can you post a repro project somewhere? – Mark Stafford - MSFT Mar 12 '15 at 02:31
  • I will have to create a Repro for you this weekend. – Prisoner ZERO Mar 12 '15 at 12:46
  • I had the same issue with crm 8.2 and the solution was first to get one record with all attributes and than i saw that my lookup name in the api is different to scheman name...... _ade_exportconsultingid_value -> ade_ExportConsultingId is the schema name once i filtered with the _ade_exportconsultingid_value the query worked. – minohimself Jul 09 '18 at 11:08

5 Answers5

42

If the OData service is of protocol version V4, the correct query URL should be:

$filter=OrganizationId eq 4c2c1c1e-1838-42ca-b730-399816de85f8

No single quotes is required.

Yi Ding - MSFT
  • 2,864
  • 16
  • 16
  • 2
    In my case, using your solution, it returns `"{\"message\":\"Syntax error at position 14 in 'Id eq 07a2c616-968b-4a73-97bc-031c401e0b07'.\"}"` Do you have any clue? – Yusril Maulidan Raji Jan 23 '18 at 10:36
  • I briefly encountered this error message when my guid was invalid (e.g. an extra character). The guid in Yusril's comment suffers from the same issue with an extra character. – nordisk Dec 27 '18 at 13:28
  • 1
    I had the same issue, but with int type - removing quotes solved the issue. – Adrian Bystrek Mar 25 '19 at 12:07
  • 2
    try [ ID eq guid'07a2c616-968b-4a73-97bc-031c401e0b07' ]. Worked for me – Eric Christoph Sep 09 '20 at 17:27
  • To add to this answer: You need to send correct type for the field. `OrganizationId` wants a GUID or string. If you are using a **custom field** that is a *string*, you likely will need to wrap `12345` in single quotes e.g. `$filter=myCustomField eq '12345'` – PotatoFarmer Apr 20 '23 at 02:28
3

I ran into this error querying OData 4.0 through Microsoft Dynamics. The other answers here didn't help unfortunately, even though they are exactly right. My issue was more with handing EntityReference's in filters.

I ended up having to adjust my filter to something like this, to target the foreign key properly. In the example below 'parentaccountid' is the foreign key in the entity I was querying. 'accountid' is the primary key in the accounts entity.

/opportunities?$select=opportunityid&$filter=parentaccountid/accountid eq 5e669180-be01-e711-8118-e0071b6af2a1
raterus
  • 1,980
  • 20
  • 23
1

Every value which is having kind of an id to another entity reference in ms crm, should be evaluated like this.

 $filter=_foodValue eq 593687F4-8B0C-E811-81B1-91CF10505DB5 

Does not require quotes or guid string.

Adrian
  • 2,825
  • 1
  • 22
  • 32
Rohit Kumar
  • 983
  • 9
  • 11
0

Please try

$filter=OrganizationId%20eq%20guid%27067e6162-3b6f-4ae2-a171-2470b63dff02%27
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
0

With PowerBi Rest API, I was able to fix the issue by putting the GUID within single quotes: https://api.powerbi.com/v1.0/myorg/groups?%24filter=id%20eq%20'9c02ab25-0e94-4835-92e6-62ac6460acd0'

Raj Rao
  • 8,872
  • 12
  • 69
  • 83