2

I'm trying to get a list of cases whose AccountID is found in a previous list.

The error occurs on the last line of the following:

// Gets the list of permissions for the current contact
var perms = ServiceContext.GetCaseAccessByContact(Contact).Cast<Adx_caseaccess>();

// Get the list of account IDs from the permissions list
var customerIDs = perms.Select(p => p.adx_accountid).Distinct();

// Get the list of cases that belong to any account whose ID is in the `customerID` list
var openCases = (from c in ServiceContext.IncidentSet where customerIDs.Contains(c.AccountId) select c).ToList();

I'm not sure what the "invalid property" is the error is talking about. The code compiles, I just get the error at runtime.

saturdayplace
  • 8,370
  • 8
  • 35
  • 39

1 Answers1

4

The problem is the CRM Linq Provider. It doesn't support all of the available options that the Linq-to-objects provider offers. In this case, the CRM does not support the Enumerable.Contains() method.

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.

You can work around this in one of two ways:

  1. Rework your query to use a more natural join.
  2. If a join is not possible, you can use Dynamic Linq to generate a list of OR clauses on each item in customerIDs. This would function similarly to Enumerable.Contains.

See my answer or the accepted answer to the question "How to get all the birthdays of today?" for two separate ways to accomplish this.

Community
  • 1
  • 1
Peter Majeed
  • 5,304
  • 2
  • 32
  • 57
  • When trying this, I get this error: An exception of type 'System.ServiceModel.FaultException`1' occurred in Microsoft.Xrm.Sdk.dll but was not handled in user code. Additional information: Generic SQL error. – Costin_T Nov 21 '16 at 09:12