好吧,我不确定我是错了还是linq解析了错误,但是如果我不使用额外的括号,则以下linq查询返回我不想要的内容.
int? userLevel;
Guid? supervisorId;
List<int> levelsIds = new List<int>();
string hierarchyPath;
// init the vars above
// ...
var qry = from f in items
where userLevel.HasValue
? (f.IsMinLevelIdNull() || (levelsIds.Contains(f.MinLevelId)))
: true
&& supervisorId.HasValue
? (f.IsSupervisorIdNull() || (!f.ChildrenVisibility && (f.SupervisorId == supervisorId.Value))
|| (f.ChildrenVisibility && (hierarchyPath.IndexOf(f.SupervisorId.ToString()) >= 0)))
: true
select f;
Run Code Online (Sandbox Code Playgroud)
这里的想法是通过变量'userLevel'和'supervisorId'的存在在两个条件块"激活"之间运行查询.
例如,如果userLevel和supervisoId都为null,则查询变为:
var qry = from f in items
where true && true
select f;
Run Code Online (Sandbox Code Playgroud)
好吧,只有当我在附加括号中包含2个三字母时,此查询才有效:
var qry = from f in items
where (userLevel.HasValue
? (f.IsMinLevelIdNull() || (levelsIds.Contains(f.MinLevelId)))
: true)
&& (supervisorId.HasValue
? (f.IsSupervisorIdNull() || (!f.ChildrenVisibility && (f.SupervisorId == supervisorId.Value))
|| (f.ChildrenVisibility && (hierarchyPath.IndexOf(f.SupervisorId.ToString()) >= 0)))
: true)
select f;
Run Code Online (Sandbox Code Playgroud)
问题是:为什么需要额外的括号?我的观点是linq解析器存在问题.
| 归档时间: |
|
| 查看次数: |
84 次 |
| 最近记录: |