Invalid LINQ CRM 2011 query: condition "Invalid", where "condition". An entity member refers to an invalid property or method. "

I am trying to execute this query to retrieve audit elements for certain types of entities

public List<Audit> GetAuditChangesSince(DateTime since, string entityType) { return (from a in OrgContext.CreateQuery<Audit>() where a.ObjectId != null && a.ObjectId.LogicalName == entityType && a.CreatedOn > since select a).ToList(); } 

a.ObjectId! = null && & The a.ObjectId.LogicalName == entityType & clause causes problems. I know .Equals () can cause problems (hence ==), and there are these limitations for the LINQ Provider:

The left side of the sentence must be the name of the attribute, and the right side of the sentence must be the value

The left side is a property, and the right is a constant. Is there a problem with .ObjectId.LogicalName?

+6
source share
2 answers

Since the audit object does not provide an earth-level attribute for the logical name / code type of the object to which a particular record belongs, it is best to associate the object (or entities) with you; you want to find audit records for - that is, without receiving all records.

A common technique for such scenarios is that you can refer to a related object with a half-saturation condition, for example, to verify that the primary key is not null. A link is enough for your case.

Example for pulling audit records tied to a contact:

 from a in OrgContext.CreateQuery<Audit>() join c in ContactSet on a.ObjectId.Id equals c.ContactId where a.ObjectId != null && a.CreatedOn > since select a 
+6
source

Add ToList() after the CreateQuery method, so your conditions will work with the LINQ to Objects provider, and not with the LINQ CRM provider (and its limitations)

 public List<Audit> GetAuditChangesSince(DateTime since, string entityType) { return (from a in OrgContext.CreateQuery<Audit>().ToList() where a.ObjectId != null && a.ObjectId.LogicalName == entityType && a.CreatedOn > since select a).ToList(); } 
+1
source

Source: https://habr.com/ru/post/945657/


All Articles