在实体框架的Where子句中使用基于枚举的var会引发异常

发布于 2024-08-02 18:35:34 字数 1124 浏览 1 评论 0原文

我有以下引发异常的代码(详细信息请参见下面的代码注释)。我只是尝试使用枚举实例作为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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

居里长安 2024-08-09 18:35:34

问题在于实体框架在构建 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文