如何使用LINQ检查值重叠

时间:2013-12-24 12:23:07

标签: c# linq entity-framework

如何避免C#实体框架中的范围重叠

select * from globalsettings 
where minvalue between 3 and 6 OR maxvalue between 3 and 6

2 个答案:

答案 0 :(得分:1)

int checkRange = (from globalSetting in db.GlobalSetting
                  where ((globalSetting.MinValue >= minValue 
                          && globalSetting.MinValue <= maxValue) 
                          || (globalSetting.MaxValue >= minValue 
                                && globalSetting.MaxValue <= maxValue))
                  select globalSetting)
                  .Count();

答案 1 :(得分:0)

接受的答案肯定很有效,但我想我会指出另一种方法,只是为了好奇,因为你提到你正在使用实体框架。 您可以使用ObjectQuery

var vals = db.GlobalSetting.Where("(minvalue between 3 and 6) or (maxvalue between 3 and 6)");
//if you just want the count of these:
int checkRange = vals.Count();