无法创建“System.Object”类型的常量值。此上下文仅支持原始类型(例如 Int32、String 和 Guid)
我正在使用 MVC 和实体框架。我使用下面的代码在模型文件夹中创建了一个类。我不断收到上面的错误消息和下面的两个查询。我知道引用非标量变量存在一个已知问题,但我不确定如何实现解决方法:
http://msdn.microsoft.com/en-us/library/bb896317.aspx#Y1442
private MovieLibraryDBEntities movieLibraryDBEntitiesContext;
public int getNumberOfEntriesReserved()
{
return (from m in movieLibraryDBEntitiesContext.Movies
where m.CheckedOut.Equals(1)
select m).Count();
//return movieLibraryDBEntitiesContext.Movies
// .Where(e => e.CheckedOut.Equals(1))
// .Select (e => e.Title).Count();
}
I'm using MVC and Entity Framework. I've created a class in my model folder with this code below. I keep getting the error message above with both queries below. I know there is a known issue on referencing non-scalar variables, but I'm not sure how to implement a workaround:
http://msdn.microsoft.com/en-us/library/bb896317.aspx#Y1442
private MovieLibraryDBEntities movieLibraryDBEntitiesContext;
public int getNumberOfEntriesReserved()
{
return (from m in movieLibraryDBEntitiesContext.Movies
where m.CheckedOut.Equals(1)
select m).Count();
//return movieLibraryDBEntitiesContext.Movies
// .Where(e => e.CheckedOut.Equals(1))
// .Select (e => e.Title).Count();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不能在 linq-to-entities 查询中使用
m.CheckedOut.Equals(1)
。使用m.CheckedOut == 1
,但CheckedOut
必须是整数
。You cannot use
m.CheckedOut.Equals(1)
in linq-to-entities query. Usem.CheckedOut == 1
butCheckedOut
must beinteger
.这是一个较老的问题。当尝试使用 IQueryable 接口过滤可为空列时,我遇到了同样的问题。我通过首先检查对象是否有值然后检查该值来解决这个问题。
This is an older question. I had the same problem when trying to filter a nullable column using the IQueryable interface. I solved the problem by first checking to see if the object had a value and then checking the value.
使用
Any()
出现同样的问题我必须更改我的 where 子句来搜索原始类型,对我来说 int
所以这
变成了这样
有一个 博客文章解释了这个怪癖。
same issue using
Any()
i had to change my where clause to search on primitive types, for me int
so this
becomes this
There is a blog post explaining the quirk.