Can LINQ be used in Dynamics CRM so that all accounts are not in the collection?

How can I write a LINQ query to return all accounts in which the account number is not listed?
The list will be extracted from the excel document.

private bool GetAccounts() { List<String> accountIds = new List<String>(); accountIds.Add( "[unknown]"); var query = from accounts in context.AccountSet where !accountIds.Contains(accounts.AccountNumber) select accounts; } 

It does not have to be a list.

EDIT

This is what happens when the above request is executed - is it a CRM error? enter image description here

+4
source share
1 answer

I do not believe you can through linq. The following are the limitations of the where clause of the SDK.

where => The left side of the sentence should be the name of the attribute, and the right side of the sentence should be the value. You cannot set the left side to constant. Both sides of a sentence cannot be constants.

Supports String functions Contains, StartsWith, EndsWith and Equals.

You can get around these limitations using QueryExpression or FetchExpressions. The query you want will look like this using QueryExpression. The only thing I would like to mention is that if you expect a lot of entries (in my opinion, 5000+), you will most likely need to implement swap for your function.

 private static IEnumerable<Account> GetAccounts(IOrganizationService proxy) { List<String> accountIds = new List<String>(new string[]{"654321", "12345"}); var results = proxy.RetrieveMultiple(new QueryExpression(Account.EntityLogicalName) { ColumnSet = new ColumnSet("accountid", "name", "accountnumber"), Criteria = new FilterExpression() { Conditions = { new ConditionExpression("accountnumber", ConditionOperator.NotIn, accountIds) } } }); return results.Entities.Select(x=> x.ToEntity<Account>()); } 
+15
source

All Articles