Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude an array in a LINQ expression that connects to CRM

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.

like image 213
ToFo Avatar asked Dec 08 '25 10:12

ToFo


1 Answers

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)

like image 82
Janus Pienaar Avatar answered Dec 10 '25 23:12

Janus Pienaar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!