简体   繁体   中英

Convert VB.NET Linq to C# Linq code

I am converting some VB.Net code over to C# and I am stuck on this linq query:

(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))

I have converted all but one small piece of code:

(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);

I am not sure how to do the part where the comments are in C#.

If you need to assign a variable you can use the LINQ let clause . Something like

let key = query.QueryKey

that can later be used in the same scope. In your case, though, I would say that just removing the var keyword from the anonymous type should allow you to reference it later in your where clause. Something like:

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))

Try the following:

((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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM