我正在将一些VB.Net代码转换为C#,我仍然坚持这个linq查询:
(From query In dtx.WebQueries Join _
wt In dtx.WebTasks On wt.TaskReportCategory Equals query.QueryCategory _
Where wt.TaskKey = taskKey _
//In the legacy code they reuse thekey below
Select query, thekey = query.QueryKey Order By query.QueryTitle _
Where Not (From qry In dtx.WebQueries Join _
qg In dtx.WebQueryGroups On qry.QueryKey Equals qg.QueryKey Join _
wp In dtx.WebPermissions On qg.QueryGroupNameKey Equals wp.TaskGroupNameKey Join _
wugn In dtx.WebUserGroupNames On wp.UserGroupNameKey Equals wugn.UserGroupNameKey Join _
wug In dtx.WebUserGroups On wugn.UserGroupNameKey Equals wug.UserGroupNameKey Join _
wt In dtx.WebTasks On wt.TaskReportCategory Equals qry.QueryCategory _
Where wp.ResourceKey = 4 _
And wt.TaskKey = taskKey _
And wug.UserKey = userKey _
//Here they reuse thekey and I am not sure how to assign it
Select qry.QueryKey).Contains(thekey))
我已经转换了除一小段代码之外的所有代码:
(from query in dtx.WebQueries join
wt in dtx.WebTasks on query.QueryCategory equals wt.TaskReportCategory
where wt.TaskKey == taskKey
//I am not sure how to assign a variable here to use later
select new { query, var key = query.QueryKey}).OrderBy(x => x.QueryTitle)
.Where(!from qry in dtx.WebQueries join
qg in dtx.WebQueryGroups on qry.QueryKey equals qg.QueryKey join
wp in dtx.WebPermissions on qg.QueryGroupNameKey equals wp.TaskGroupNameKey join
wugn in dtx.WebUserGroupNames on wp.UserGroupNameKey equals wugn.UserGroupNameKey join
wug in dtx.WebUserGroups on wugn.UserGroupNameKey equals wug.UserGroupNameKey join
wt in dtx.WebTasks on qry.QueryCategory equals wt.TaskReportCategory
where wp.ResourceKey == 4
&& wt.TaskKey == taskKey
&& wug.UserKey == userKey
select qry.QueryKey) == key //I need to put the variable here (see above comment);
我不知道如何处理C#中的注释部分。
答案 0 :(得分:1)
如果需要分配变量,可以使用LINQ let clause。像
这样的东西let key = query.QueryKey
以后可以在同一范围内使用。但是,在您的情况下,我会说只是从匿名类型中删除var关键字应该允许您稍后在where子句中引用它。类似的东西:
select new { query, key = query.QueryKey}).OrderBy(x => x.query.QueryTitle)
.Where(q => !(from qry in dtx.WebQueries join
qg in dtx.WebQueryGroups on qry.QueryKey equals qg.QueryKey join
wp in dtx.WebPermissions on qg.QueryGroupNameKey equals wp.TaskGroupNameKey join
wugn in dtx.WebUserGroupNames on wp.UserGroupNameKey equals wugn.UserGroupNameKey join
wug in dtx.WebUserGroups on wugn.UserGroupNameKey equals wug.UserGroupNameKey join
wt in dtx.WebTasks on qry.QueryCategory equals wt.TaskReportCategory
where wp.ResourceKey == 4
&& wt.TaskKey == taskKey
&& wug.UserKey == userKey
select qry.QueryKey).Contains(q.key))
答案 1 :(得分:0)
尝试以下方法:
((from query in dtx.WebQueries
join wt in dtx.WebTasks on wt.TaskReportCategory equals query.QueryCategory
where wt.TaskKey == taskKey
select new {query, thekey = query.QueryKey}).OrderBy(query => query.QueryTitle)
where!(
from qry in dtx.WebQueries
join qg in dtx.WebQueryGroups on qry.QueryKey equals qg.QueryKey
join wp in dtx.WebPermissions on qg.QueryGroupNameKey equals wp.TaskGroupNameKey
join wugn in dtx.WebUserGroupNames on wp.UserGroupNameKey equals wugn.UserGroupNameKey
join wug in dtx.WebUserGroups on wugn.UserGroupNameKey equals wug.UserGroupNameKey
join wt in dtx.WebTasks on wt.TaskReportCategory equals qry.QueryCategory
where wp.ResourceKey == 4 && wt.TaskKey == taskKey && wug.UserKey == userKey
select qry.QueryKey).Contains(thekey));