1

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#.

5
  • 1
    I need to put the variable here <- which variable, how? Commented Jul 24, 2014 at 20:55
  • 1
    How about formatting your code so that the line breaks occur in the same relative location in both languages? Commented Jul 24, 2014 at 20:56
  • @PieterGeerkens Sorry I use an Authohotkeyscript to fix all the lines and remove the _ and I didn't even think about it. Commented Jul 24, 2014 at 20:59
  • You could always try a conversion site to see if that helps you. I'll do that every once in a while to see if it can get me moving in the right direction. Just do so with a grain of salt. I've used: developerfusion.com/tools/convert/csharp-to-vb to good effect. Commented Jul 24, 2014 at 21:05
  • @Yatrix I used that one and the telerik one. They both errored out on thekey = query.QueryKey Commented Jul 24, 2014 at 21:07

2 Answers 2

1

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))
Sign up to request clarification or add additional context in comments.

1 Comment

it's out of scope by that point since in C# I have to close the first from statement after the first select (unless there is another way)
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.