Конвертировать VB.NET Linq в C # Linq код

1

Я конвертирую код VB.Net в С#, и я зациклился на этом запросе 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);

Я не уверен, как сделать ту часть, где комментарии находятся на С#.

  • 1
    I need to put the variable here <- какая переменная, как?
  • 1
    Как насчет форматирования кода, чтобы разрывы строк происходили в одном и том же относительном месте на обоих языках?
Показать ещё 3 комментария
Теги:
linq

2 ответа

1
Лучший ответ

Если вам нужно назначить переменную, вы можете использовать предложение LINQ let. Что-то вроде

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))
  • 0
    это выходит за рамки этого вопроса, так как в C # я должен закрыть первый оператор from после первого выбора (если нет другого пути)
  • 0
    @ Роберт, я обновил свой ответ, посмотри, сработает ли это для тебя.
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));

Ещё вопросы

Сообщество Overcoder
Наверх
Меню