例外をスローする次のコードがあります (以下のコード コメントの詳細)。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 にコピーするよりも洗練された解決策はありますか?