简体   繁体   中英

Entity framework, is it possible to use substract in where clause

I have 2 database fields and there is need to filter results based on those values.

As an example customers max storage places and storage places in use. To determine if someone is using more space I would like to make like this.

var temp = db.Storege.Where(o => (o.max_storage_places - o.places_in_use) < 0).ToList()

But this is not working. Is this possible or should I use different approach?

EDIT:

Database is MySQL and it is used trough Entity Framework. The datatype for both fields are INT values in database.

I tested that following version is working which was suggested

var temp = db.Storege.Where(o => o.max_storage_places < o.places_in_use).ToList()

ROOT CAUSE:

I was investigating this problem more deeply and found out that this is MySQL related issue. The fields were for some reason "Unsigned INT" so for database didn't understand the negative values in calculation.

Both cases will work

Yes,you can do it.But you have to use ToList() to fetch the records from the database.

var temp = db.Storege.Where(o => (o.max_storage_palces - o.places_in_use) < 0).ToList();

OR

var temp = db.Storege.Where(o => (o.max_storage_palces < o.places_in_use ).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