C#linq与selectMany to sql语句 [英] c# linq with selectMany to sql statement

查看:242
本文介绍了C#linq与selectMany to sql语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何将此linq代码转换为普通的SQL查询.我知道第一部分,我需要进行内部连接,但是我不知道具体之处.

I dont know how to translate this linq code to a normal SQL query. I know the first part and that I need to make an inner join but I dont know where exaclty.

此linq查询在SQL查询中的外观如何?

How does this linq query Looks like in SQL query?

context.DataTable.FirstOrDefault(x => x.ID == ID).Roles.SelectMany(x => x.RoleToExtensions.Select(y => y.Extension)).ToList();

推荐答案

在您的DbContext类中添加以下代码并运行该代码.该查询将显示在Visual Studio的输出窗口中. (需要安装nuget包Microsoft.Extensions.Logging和Microsoft.Extensions.Logging.Console,并添加相应的命名空间)

Add the below code in your DbContext class and run the code. The query will be shown in the output window of Visual Studio. (need to install nuget package Microsoft.Extensions.Logging and Microsoft.Extensions.Logging.Console, and add the respective namespaces)

      public static readonly LoggerFactory MyLoggerFactory
          = new LoggerFactory(new[] { new ConsoleLoggerProvider((_, __) => true, true) });


      protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
      {
#if DEBUG
         optionsBuilder
             .UseLoggerFactory(MyLoggerFactory)
             .EnableSensitiveDataLogging();
#endif
      }

或者如果您只需要LINQ查询的简单sql:

Or if you just need a simple sql of the LINQ query:

SELECT re.Extension FROM roletoextension re 
WHERE re.RoleId IN 
(SELECT RoleId FROM Role r 
WHERE r.DataTableId = {ID})

这篇关于C#linq与selectMany to sql语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆