public IEnumerable<User> GetUsers(string location, string role, bool excludeHeadOffice )
{
var users = from u in DataContext.Users
where
(u.location == location || location == null) &&
(u.role == role || role == null)
return users;
}
我想要做的是,还有一个像这样的额外过滤器:
(excludeHeadOffice && u.location != "ho")
如何将此表达式添加到现有的where子句?
更新的 如果excludeHeadOffice为true,则应使用过滤器
(excludeHeadOffice && u.location != "ho")
否则它应该像以前一样使用:
(u.location == location || location == null)
答案 0 :(得分:1)
你只需要仔细思考并提出必要的布尔逻辑:
df = read.table(text = "shop_id,item_id,time,value
150,1,2015-07-10,3
150,1,2015-07-11,5
150,1,2015-07-13,2
150,2,2015-07-10,15
150,2,2015-07-12,12", header = TRUE, sep = ",")
答案 1 :(得分:0)
这是你在找什么?
public IEnumerable<User> GetUsers(string location, string role, bool excludeHeadOffice = false)
{
var users = from u in DataContext.Users
where
(u.location == location || location == null) &&
(u.role == role || role == null)
if(excludeHeadOffice)
{
return users.Where(u => u.location != "headOfficeLocation" && u.location !="ho");
}
return users;
}
答案 2 :(得分:0)
在多个语句中构建您的查询。
使用扩展方法可以(可以说)更容易构建它:
var query = DataContext.Users.AsQueryable();
if (excludeHeadOffice)
query = query.Where(x => x.location != "ho");
else if (location != null)
query = query.Where(x => x.location == location);
if (role != null)
query = query.Where(x => x.role == role);
return query;
这里的想法是你从一个基地开始&#34;让我得到一切&#34;查询,并继续添加if语句。