Tuesday, January 24, 2012

LINQ Restrictions with CRM 2011 xRM Provider

Apparently there are different rules with the LINQ provider in the CRM 2011 SDK from the CRM 4.0 LINQ provider, and I ran into one today.

I have a process were I am deleting old email activities that meet a specific criteria in the subject line.  I had a LINQ query in CRM 4.0 console app that worked just fine:


var Emails = (
 from e1 in xrmConnection.EmailSet
 join i in xrmConnection.IncidentSet on e1.RegardingObjectId.Id equals i.IncidentId
 where e1.CreatedOn.Value <= dt
 && e1.Subject.ToString().StartsWith("XYZ:", true, null) 
 where i.IncidentStageCode.Value == 200999 && i.Custom_ResolvedOn.Value <= dt
 select e1).Take(MaxRecordsToProcess).ToList();


When I ran this same LINQ query in CRM 2011, it didn't work and threw this error:

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


After a search, I found this forum post that explained the error and after trial and error I found my specific problem.  The key point is this forum post is this limitation statement:
A limitation of the CRM LINQ provider is that:


    1.The left hand side of a predicate (where clause) MUST be an entity attribute 
    2.The right hand side of a predicate MUST be a literal value or variable 



At first, I didn't see it because I was putting them in the right order...the problem was the the string check didn't actually compare it to 'true'.


var Emails = (
 from e1 in xrmConnection.EmailSet
 join i in xrmConnection.IncidentSet on e1.RegardingObjectId.Id equals i.IncidentId
 where e1.CreatedOn.Value <= dt
 && e1.Subject.ToString().StartsWith( "XYZ:" , true, null) == true
 where i.IncidentStageCode.Value == 200999 && i.Custom_ResolvedOn.Value <= dt
 select e1).Take(MaxRecordsToProcess).ToList();


Adding that '== true' to the string comparison solved it.   

Weird.

No comments:

Post a Comment