NotSupportedException: Invalid 'where' condition. An entity member is invoking an invalid property or method.
Hi,
I want to share an odd exception I got today while trying a LINQ query within a Plugin through the LINQ query provider in Microsoft Dynamics CRM 2011.
When running the following query:
public static PriceLevel GetPriceLevel(string priceLevelName, IOrganizationService organizationService)
{
var crm = new Xrm.MyXrmServiceContext(organizationService);
var PriceLevelToreturn = crm.PriceLevelSet.Where(p => priceLevelName.ToLower().Equals(p.Name.ToLower()) ).FirstOrDefault();
return PriceLevelToreturn;
}
.. I ran into the following Exception:
NotSupportedException: Invalid 'where' condition. An entity member is invoking an invalid property or method.
Nothing were wrong with the types I was comparing to. Everything seemed to be fine until I came up with this article that depicts some limitations regarding to the LINQ query provider in Microsoft Dynamics CRM 2011:
where
The left side of the clause must be an attribute name and the right side of the clause must be a value. You cannot set the left side to a constant. Both the sides of the clause cannot be constants.
Supports the String functions Contains, StartsWith, EndsWith, and Equals.
Really annoying and unknown up to this moment for me :( . So, based on these statements I had two wrong things in my query:
1. I was using the attribute at the right side and it should be located at the left (pretty curious why btw )
2. I was using .ToLower() method which seems no to be supported.
So, I had to correct the above couple of things and after that, my code looked like this:
public static PriceLevel GetPriceLevel(string priceLevelName, IOrganizationService organizationService)
{
var crm = new Xrm.MyXrmServiceContext(organizationService);
var PriceLevelToreturn = crm.PriceLevelSet.Where(p => p.Name.Equals(priceLevelName)).FirstOrDefault();
return PriceLevelToreturn;
}
When running again, voila! . LINQ query run successfully.
Hope it helps to save somebody else time,
PP [twitter: @pabloperalta]
UruIT Dynamix | Excellence in Dynamics CRM Nearshoring Services.
PS: Luckily in my case I didn’t actually need ToLower() in the end.