在实体框架的Where子句中使用基于枚举的var会引发异常
我有以下引发异常的代码(详细信息请参见下面的代码注释)。我只是尝试使用枚举实例作为Where 子句的一部分。我理解该消息,但不明白为什么 EF 无法解析 Int32 枚举。
如果我将枚举复制到 Int32 然后对其进行过滤,它会起作用,但看起来很混乱。
Enum MyEnum As Int32
Zero
One
Two
End Enum
Shared Function GetStuff(ByVal EnumValue As MyEnum) As IQueryable
Dim Db As New MainDb
Dim DetailList As IQueryable
Dim MyInt As Int32 = EnumValue
' PostalProviderId is an Int column in SQL.
'DetailList = From D In Db.DeliveryService Where D.PostalProviderId = EnumValue ' This fails.
DetailList = From D In Db.DeliveryService Where D.PostalProviderId = MyInt ' This works.
' The following attempt to enumerate the results yields;
' **** System.NotSupportedException was unhandled by user code
' **** Message = "Unable to create a constant value of type 'Closure type'. Only primitive types ('such as Int32, String, and Guid') are supported in this context."
' **** Source = "System.Data.Entity"
For Each Thingy In DetailList
Console.WriteLine(Thingy.ToString())
Next
Return DetailList
End Function
有没有比将枚举值复制到本地 int 更优雅的解决方案?
I have the following code which throws an exception (detail in code comments below). I am merely trying to use an instance of an enum as part of the Where clause. I understand the message, but I don't understand why EF cannot parse an Int32 enum.
It works if I copy the enum to an Int32 and then filter on that, but it seems very messy.
Enum MyEnum As Int32
Zero
One
Two
End Enum
Shared Function GetStuff(ByVal EnumValue As MyEnum) As IQueryable
Dim Db As New MainDb
Dim DetailList As IQueryable
Dim MyInt As Int32 = EnumValue
' PostalProviderId is an Int column in SQL.
'DetailList = From D In Db.DeliveryService Where D.PostalProviderId = EnumValue ' This fails.
DetailList = From D In Db.DeliveryService Where D.PostalProviderId = MyInt ' This works.
' The following attempt to enumerate the results yields;
' **** System.NotSupportedException was unhandled by user code
' **** Message = "Unable to create a constant value of type 'Closure type'. Only primitive types ('such as Int32, String, and Guid') are supported in this context."
' **** Source = "System.Data.Entity"
For Each Thingy In DetailList
Console.WriteLine(Thingy.ToString())
Next
Return DetailList
End Function
Is there a more elegant solution than copying the enum values to a local int?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于实体框架在构建 T-SQL 以获得其背后的 int 时不知道如何评估枚举。简而言之,您必须将其存储在临时变量中并使用它。
更多信息请访问:
http: //gmontrone.com/post/problem-with-casting-enums-in-linq-to-entities.aspx
和
http://www.matthidinger.com/archive/2008/02/26/entity-framework-comparison-frustration-explained。 ASPX
The problem is that the entity framework doesn't know how to eveluate your enum when it is building the T-SQL to get the int behind it. The short answer is that you have to store it in a temp variable and use that.
Some more information can be found at:
http://gmontrone.com/post/problem-with-casting-enums-in-linq-to-entities.aspx
and
http://www.matthidinger.com/archive/2008/02/26/entity-framework-comparison-frustration-explained.aspx