c# - 在连接到 CRM 的 LINQ 表达式中排除数组

标签 c# linq visual-studio-2017 dynamics-crm-2016

我正在尝试运行一个 LINQ 查询,该查询获取特定类型 [new_contact] 的所有对象,但过滤掉那些具有与列表 [excludeIdentifiers] 匹配的特定字段值 [new_identifier] 的对象。

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;
    }
}

我收到这个错误:

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>

我确定错误是由第五行引起的(因为没有它它运行良好):

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

我在本地使用 Visual Studio 2017 和 CRM 2016 以及 Windows 10。Google 返回给我的答案至少已有七年历史,不够具体且不适合我。

最佳答案

错误消息准确地描述了您的问题,您正在尝试将 Int 值与 array of Int 进行比较。

您需要更改行 !(a.new_entity_identifier.Value.Equals(excludedIdentifiers))

类似于 !excludedIdentifiers.Contains(a.new_entity_identifier.Value)

关于c# - 在连接到 CRM 的 LINQ 表达式中排除数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44539693/

相关文章:

c# - C#中的泛型约束,T是一样的TSomethingElse吧?

c# - GDI+ 中两个形状之间的空白区域

c# - 在 Linq 中按问题分组

c# - 是否可以查询 ExpandoObject 列表?

c# - Linq Sum() 精度

c# - 如何使用 MsBuild v15 构建 C++ 项目?

c# - 将 Base64 图像字符串从 objective-c 上传到 MVC .Net C#

c# - 通过 TCP 在 C++ 客户端/C# 服务器之间传输文件

c++ - FileStore::OpenErr 在内存位置 0x012FED64

asp.net - Visual Studio 2017错误: Unable to start program,操作在当前状态下不合法