`

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.


代码是这样的:
ctx.Products.Where(p => p.Status == (int)s).ToString();

其中s是类型为ProdcutStatus的枚举类型:
public enum ProdcutStatus{
Open,
Close
}

这是因为Linq to Entities根据Where中的委托生成SQL语句,所以对里面的复杂程度(方法)有一定的限制,其中的(int)s就无法被正确翻译。
要解 决这个问题,需要把这个(int)s过程放到外面来:

int status = (int)s;
ctx.Products.Where(p => p.Status == status).ToString();

这样Where内部还是保持了相对的“干净”,不会阻 碍SQL语句的动态生成。

分享到:
评论

相关推荐

    Map.Framework.A.Formal.Model.of.Maps

    The book provides a complete example of mathematically defining a data type, ensuring closure properties of those operations, and then translating that type into a state that is suited for ...

    Compiler_closure_design.zip

    本项目"Compiler_closure_design.zip"聚焦于这两种自动机之间的转换与优化,采用Java编程语言实现。 首先,我们来理解NFA和DFA的基本概念。NFA是一种允许有多个转移状态的自动机,对于一个输入符号,它可能有零个或...

    closure-compiler-v20171112.jar

    Closure Compiler是Google开发的一款强大的JavaScript代码优化工具,其主要功能是对JavaScript代码进行压缩和混淆,以提高代码的运行效率和安全性。"closure-compiler-v20171112.jar"是该编译器的一个特定版本,发布...

    closure dependency not found解决包

    当我们从github上下载了blockly之后,打卡demos下的index.html时,选择blockly-developer-tools时会弹出一个对话框(大体内容是closure dependency not found),此时我们需要下载这个文件,解压并且命名为closure-...

    Closure-Table-ClosureTable.rar

    Closure Table 是数据库设计中一种处理层次结构数据的方法,主要用于存储具有层级关系的数据,例如组织架构、类别层次等。在这个名为 "Closure-Table-ClosureTable.rar" 的项目中,开发者使用了Spring、SpringMVC...

    Determination of the Instants of Glottal Closure from Speech Wave

    Determination of the instants ofglottal closure (GC) from speech wave using wavelet transform is equivalent to finding a particular local modulus maxima pattern across several scales in the time-scale...

    ug938-vivado-design-analysis-closure-tutorial.pdf

    本教程“Design Analysis and Closure Techniques”(UG938 v2022.2)旨在帮助用户深入了解如何在Vivado环境中有效地分析和优化设计,以确保其性能和合规性。 教程概述: 本教程涵盖了多个关键主题,包括如何使用...

    python学习-07-closure-deco.zip

    Python是一种解释型的、面向对象的、带有动态语义的高级程序设计语言。它是由荷兰人吉多·罗萨姆于1989年发布的,第一个公开发行版发行于1991年。Python注重解决问题的方法,而不是语法和结构。...

    理解javascript函数式编程中的闭包(closure)_.docx

    本篇文章主要探讨JavaScript函数式编程中的一个重要概念——闭包(closure)。闭包是一种特殊的函数,它能记住其定义时的作用域,即使在函数执行完毕后,仍然可以访问到该作用域内的变量。在JavaScript中,每个函数...

    Closure Library

    The Closure Library is a broad, well-tested, modular, and cross-browser JavaScript library. You can pull just what you need from a large set of reusable UI widgets and controls, and from lower-level ...

    Closure The Definitive Guide

    If you're ready to use Closure to build rich web applications with JavaScript, this hands-on guide has precisely what you need to learn this suite of tools in depth. Closure makes it easy for ...

    closure闭包

    在编程语言理论中,"closure"一词通常指的是“闭包”,它是一个非常重要的概念,尤其是在函数式编程和动态类型语言中。闭包是函数和与其相关的引用环境(即变量的值)的组合,这个组合使得函数能够记住它被定义时的...

    Skiena-The_Algorithm_Design_Manual.pdf

    2.8 War Story: Mystery of the Pyramids . . . . . . . . . . . . . . . . 51 2.9 Advanced Analysis (*) . . . . . . . . . . . . . . . . . . . . . . . . 54 2.10 Exercises . . . . . . . . . . . . . . . . . ...

    Service Support-英文原版

    1.9 A Code of Practice for IT Service Management – PD0005....................- 10 - 1.10 Service Management: a process approach.................................................................- 11 - ...

    Contact, closure and friction behaviour of rough crack concrete surfaces.pdf

    标题《Contact, closure and friction behaviour of rough crack concrete surfaces.pdf》表明本文主要研究对象是混凝土裂缝表面的接触特性、裂缝闭合行为以及摩擦行为。 描述中提到的《Contact, closure and ...

    (带笔记注释版)Detection of Glottal Closure Instants from Speech Signals

    标题和描述中所提及的内容主要围绕了声门闭合时刻(Glottal Closure Instants, GCI)的检测方法,并且特别指出了使用卷积神经网络(CNN)来进行分类的重要性。为了深入理解,我们首先来拆解这几个关键概念。 声门...

    nodejs中文学习手册

    2.2 Scope 與 Closure . . . . . . . . . . . . . . . . . . . . . . . . . . 12 2.3 Callback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 2.4 CPS(Continuation-Passing Style) . . . ...

    Collections

    Pointer arrays are a special type of array that holds pointers to objects rather than the objects themselves. They are used when direct memory management is required or when working with Core ...

    loop_closure_闭环检测_loopclosure_

    在`loop_closure.cpp`这个文件中,可能包含了实现闭环检测的关键代码,包括特征提取、匹配、验证和位姿图优化等步骤。开发者可能使用了一些特定的库和算法,如OpenCV进行特征处理,PCL(Point Cloud Library)处理...

Global site tag (gtag.js) - Google Analytics