I have an enumeration of [Flags] as follows:
[Flags] public enum Status { None = 0, Active = 1, Inactive = 2, Unknown = 4 }
The state list can contain two values, for example:
Status s = Status.Active | Status.Unknown;
Now I need to create a linq query (LINQ to ADO.NET Entities) and query for records whose status is higher, that is, Active or Unknown;
var result = from r in db.Records select r where (r.Status & (byte)s) == r.Status
Of course, I get an error because LINQ to Entities knows how to handle primitive types in the Where clause.
Error:
Unable to create a constant value of type "Closure Type". Only primitive types (such as Int32, String, and Guid ') are supported in this context.
Is there a workable way? I can have an Enum status with 10 possible values โโand request 5 statuses. How can I build a query using flag renaming in an elegant way?
Thanks.
Update
This seems to be a Linq to Entities issue. I think it works in LINQ to SQL (not sure, not tested).
source share