Invalid ‘where’ condition. An entity member is invoking an invalid property or method.

Since CRM 2011 came out I have been working on a lot of Silverlight applications that interface with it. I haven’t really had the chance of using LINQ all to much to query CRM. The other day I found myself working on a plugin and creating a complex LINQ query, YAY! When it came to testing my query I recieved the following error message on execution “Invalid ‘where’ condition. An entity member is invoking an invalid property or method.”.

My first thought was that I must have specified a property incorrectly… Nope!

My second thought was that maybe I was comparing incompatible objects… Nope!

I decided to comment out my where clauses and bring them back in one by one to find the culprit. I found the line that was causing the issue, it turns out having the CRM Property on the right hand side of the where clause was causing it to crash! I’m not the greatest with LINQ but having to keep the CRM properties on the left hand side of the where clauses seems a bit strange to me! Maybe this is an error with the LINQ to CRM implementation?

Anyway let me show you with a simple example. Lets say we want to query contacts and bring back all contacts whos age is greater than 20. Age is a custom field we have created on contact.

The following will crash with “Invalid ‘where’ condition. An entity member is invoking an invalid property or method.”

public IList GetContacts(int age)
{
var query = from c in OrgService.CreateQuery("contact")
where age < (int)c["cc_age"]
select c;

return query.ToList();
}

Where as the following will execute without error

public IList GetContacts(int age)
{
var query = from c in OrgService.CreateQuery("contact")
where (int)c["cc_age"] > age
select c;

return query.ToList();
}

Leave a Reply

Your email address will not be published. Required fields are marked *