简体   繁体   English

实体框架,可以在where子句中使用减法

[英]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. 我有2个数据库字段,需要根据这些值过滤结果。

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. 数据库是MySQL,通过实体框架使用。 The datatype for both fields are INT values in database. 这两个字段的数据类型均为数据库中的INT值。

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. 我正在对这个问题进行更深入的调查,发现这是与MySQL相关的问题。 The fields were for some reason "Unsigned INT" so for database didn't understand the negative values in calculation. 字段由于某种原因是“ Unsigned INT”,因此对于数据库而言,它们无法理解计算中的负值。

Both cases will work 两种情况都可以

Yes,you can do it.But you have to use ToList() to fetch the records from the database. 是的,您可以做到。但是您必须使用ToList()从数据库中获取记录。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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