Unable to create a constant value of type 'Closure type'.异常,如何解决?
[已解决] Unable to create a constant value of type 'Closure type'.异常,如何解决?
当前页面:http://www.senparc.com/SZD-59
{ 收藏当前页面 }
0
[软件及编程 > C# > LINQ > LINQ to Entities]
使用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
}
如果不加这个Where就没有错,所以猜想是Where(p => p.Status == (int)s)中间的问题,请问这是什么问题呢?如何解决?
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
}
如果不加这个Where就没有错,所以猜想是Where(p => p.Status == (int)s)中间的问题,请问这是什么问题呢?如何解决?
最佳答案
这是因为Linq to Entities根据Where中的委托生成SQL语句,所以对里面的复杂程度(方法)有一定的限制,其中的(int)s就无法被正确翻译。
要解决这个问题,需要把这个(int)s过程放到外面来:
int status = (int)s;
ctx.Products.Where(p => p.Status == status).ToString();
这样Where内部还是保持了相对的“干净”,不会阻碍SQL语句的动态生成。
要解决这个问题,需要把这个(int)s过程放到外面来:
int status = (int)s;
ctx.Products.Where(p => p.Status == status).ToString();
这样Where内部还是保持了相对的“干净”,不会阻碍SQL语句的动态生成。
回答时间:2010/4/19 17:29:44
| 回答者:zsu
其他参考答案(0)
提交失败!请检查错误!错误信息:
以下信息或许对您有用:
- [已解决] 100 Unable to update the EntitySet 'Users' b... 2011/1/14 21:47:44
- [已解决] 40 无法创建类型为“结束类型”的常量值。此上下文仅支持基元类型(“例如 Int32、... 2010/9/19 22:28:05
- [已解决] 5 Linq to Entities是否支持SQLite数据库? 2010/9/9 23:00:55
- [已解决] 5 System.NotSupportedException: 2010/5/30 11:48:41
- [已解决] 0 The ConnectionString property has not be... 2010/5/16 23:04:39