我目前正在运行以下查询:
var results = from c in _context.Cs
join a in _context.Ca on c.Id equals a.CId
where c.Status == "A"
where c.CtId == ctId
where c.FY == fYear
where (a.PMId == lUserId || a.TLId == lInUserId)
select new { c.Id, c.T, c.C, c.S } into x
group x by new {x.Id, x.T, x.C, x.S} into g
orderby g.Key.T, g.Key.C, g.Key.S
select new { Id = g.Key.Id, T = g.Key.T, C = g.Key.C, S = g.Key.S}
现在我需要使where (a.PMId == lUserId || a.TLId == lInUserId)
行以if lUserId != 0为条件(如果不是0,请使用它,如果0则忽略它)。
通常,我会声明变量results
,然后在if语句中设置它,但是我不知道如何定义这个数据结构。它被定义为:
IQueryble<'a>
'a is new { int Id, string T, string C, string S}
做这件事最好的方法是什么?
发布于 2017-02-03 07:42:43
您可以在查询中使用||
运算符,因此如果第一个条件为真,则不会计算第二个条件,如果第一个条件为false,则将计算不等于0秒的第二个条件:
where lUserId ==0 || (a.PMId == lUserId || a.TLId == lInUserId)
发布于 2017-02-03 07:42:59
我所了解的是:
a.PMId == lUserId || a.TLId == lInUserId
当且仅当UserId != 0
lUserId ==0
正确理解需求,则忽略该条件,那么&&
将是您必须在这里使用的运算符。因为它将跳过检查第二个条件,如果第一个条件为false(即UserId == 0
)。我想你是在找这个
where (UserId != 0 && a.PMId == lUserId || a.TLId == lInUserId)
https://stackoverflow.com/questions/42019104
复制相似问题