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 にコピーするよりも洗練された解決策はありますか?

4

1 に答える 1

3

問題は、エンティティフレームワークがT-SQLを構築してその背後にあるintを取得するときに、列挙型を評価する方法を知らないことです。簡単に言うと、一時変数に格納して使用する必要があるということです。

詳細については、次のURLをご覧ください。

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

于 2009-08-25T12:48:49.937 に答える