Exclude an array in a LINQ expression that connects to CRM

3

I am trying to run a LINQ query that gets all objects of a specific type [new_contact], but filters out those with a specific field value [new_identifier] that matches in a list [excludeIdentifiers].

public static List<new_placement>ActivityCounter(Account account, CRM2011DataContext CRMcontext)
{
    int[] excludedIdentifiers = { 6, 7, 8, 9, 13, 14, 19, 20, 38, 39, 40, 41, 42, 43, 44, 45 };
    var myActivities = (from a in CRMcontext.new_placementSet
                             where
                             !(a.new_entity_identifier.Value.Equals(excludedIdentifiers))
                                 select new new_placement() { ActivityId = a.ActivityId }).ToList();
        return myActivities;
    }
}

I get this error:

System.ServiceModel.FaultException`1 occurred
  HResult=0x80131501
  Message=Condition for attribute 'new_placement.new_entity_identifier': expected argument(s) of type 'System.Int32' but received 'System.Int32[]'.
  Source=<Cannot evaluate the exception source>
StackTrace:<Cannot evaluate the exception stack trace>

I am certain that the error is caused by the fifth line (because it runs through fine without it):

!(a.new_entity_identifier.Value.Equals(excludedIdentifiers))

I am using Visual Studio 2017 and CRM 2016 on premise and Windows 10. Google returns me answers that are at least seven years old, not concrete enough and not working for me.

c#
linq
visual-studio-2017
dynamics-crm-2016
asked on Stack Overflow Jun 14, 2017 by ToFo • edited Jun 14, 2017 by Peter Mortensen

2 Answers

5

The error message describes your problem exactly, you are trying to compare an Int value to an array of Int.

You need to change the line !(a.new_entity_identifier.Value.Equals(excludedIdentifiers))

to something like !excludedIdentifiers.Contains(a.new_entity_identifier.Value)

answered on Stack Overflow Jun 14, 2017 by Janus Pienaar
0

I was able to get this working in a more 'manual' fashion. Its neither pretty or flexible but it works.

public static List<new_placement>ActivityCounter(Account account, CRM2011DataContext CRMcontext)
{
var myActivities = (from a in CRMcontext.new_placementSet
                         where
                         a.new_entity_identifier.Value != 6
                         a.new_entity_identifier.Value != 7
                         a.new_entity_identifier.Value != 8
                         a.new_entity_identifier.Value != 9 // And so on..
                             select new new_placement() { ActivityId = a.ActivityId }).ToList();
    return myActivities;
}
answered on Stack Overflow Jun 15, 2017 by ToFo • edited Jun 15, 2017 by ToFo

User contributions licensed under CC BY-SA 3.0