user4845680 user4845680 - 9 months ago 59
C# Question

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)

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


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)

Answer Source

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();


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