• Unable to create a constant value of type 'Closure type'


    使用Linq to Entities的时候发生如下异常:

    Unable to create a constant value of type 'Closure type'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.

    1. var histroyList = data.TransMemoryHistory.Where(c => c.TransMemorys.TMID == tmid && c.UsedType != (int)usedType);

    termUsedType是枚举类型。

    这是因为Linq to Entities根据Where中的委托生成SQL语句,所以对里面的复杂程度(方法)有一定的限制,其中的(int)s就无法被正确翻译。但是也不完全会出现此错误。

    要解 决这个问题,需要把这个(int)s过程放到外面来:

    1. int usedType = (int)termUsedType;
    2. var histroyList = data.TransMemoryHistory.Where(c => c.TransMemorys.TMID == tmid && c.UsedType != usedType);
  • 相关阅读:
    推导式
    解构
    for 循环
    运算符
    while 循环
    流程控制语句
    索引和切片
    ASC转换BCD,ASC2BCD(转)
    CString和char互转,十六进制的BYTE转CString
    C++添加简单的日记记录
  • 原文地址:https://www.cnblogs.com/suizhikuo/p/4960212.html
Copyright © 2020-2023  润新知