简体   繁体   中英

How to reference enum type in entity sql

I have the following (simplified) Entity SQL query:

SELECT VALUE a
FROM Customers AS a
WHERE a.Status NOT IN { 2, 3 }

The Status property is an enumeration type, call it CustomerStatus . The enumeration is defined in the EDMX file.

As it is, this query doesn't work, throwing an exception to the effect that CustomerStatus is incompatible with Int32 (its underlying type is int). However, I couldn't find a way to define a list of CustomerStatus values for the IN {} clause, no matter what namespace I prefixed to the enumeration name. For example,

SELECT VALUE a
FROM Customers AS a
WHERE a.Status NOT IN { MyModelEntities.CustomerStatus.Reject, MyModelEntities.CustomerStatus.Accept }

did not work, throwing an exception saying it could not find MyModelEntities.CustomerStatus in the container, or some such.

Eventually I resorted to casting the Status to int, such as

SELECT VALUE a
FROM Customers AS a
WHERE CAST(a.Status AS System.Int32) NOT IN { 2, 3 }

but I was hoping for a more elegant solution.

Oooh, you are writing Entity SQL directly. I see... Any reason you aren't using DbSet instead of writing Entity SQL by hand? Could always do

var statuses = new [] { Status.A, Status.B };
var query = context.SomeTable.Where(a => !statuses.Contains(a.Status)).ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM