C# Linq解析器问题?
那么, 我不确定我是否错了,或者linq解析是否有错误,但是如果我不使用额外的括号,下面的linq查询将返回我不想要的结果C# Linq解析器问题?,c#,linq,C#,Linq,那么, 我不确定我是否错了,或者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 userL
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;
好的,只有当我将两个三角图括在附加括号中时,此查询才能正常工作:
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;
问题是:为什么需要额外的括号?我的意见是linq解析器中存在问题。来自
&&
在之前得到评估:代码>由于优先顺序计算错误,因此需要额外的括号。例如,您的代码将按以下顺序计算,因为true和&supervisorId.HasValue…
var qry = from f in items
where
1st: userLevel.HasValue
?
2nd: (f.IsMinLevelIdNull() || (levelsIds.Contains(f.MinLevelId)))
:
3rd: true && supervisorId.HasValue
? (f.IsSupervisorIdNull() || (!f.ChildrenVisibility && (f.SupervisorId == supervisorId.Value))
|| (f.ChildrenVisibility && (hierarchyPath.IndexOf(f.SupervisorId.ToString()) >= 0))) **)**
: true
select f;
与林克无关。解开标签。问题是关于LINQ的,答案不是。重新标记。
var qry = from f in items
where
1st: userLevel.HasValue
?
2nd: (f.IsMinLevelIdNull() || (levelsIds.Contains(f.MinLevelId)))
:
3rd: true && supervisorId.HasValue
? (f.IsSupervisorIdNull() || (!f.ChildrenVisibility && (f.SupervisorId == supervisorId.Value))
|| (f.ChildrenVisibility && (hierarchyPath.IndexOf(f.SupervisorId.ToString()) >= 0))) **)**
: true
select f;