如何从 linq 查询中查看生成的 sql

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6190620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 10:48:23  来源:igfitidea点击:

how to see generated sql from a linq query

sqllinq

提问by RayLoveless

Just trying to get the sql that is generated by a linq query.

只是试图获取由 linq 查询生成的 sql。

回答by sgmoore

With Linq2Sql

使用 Linq2Sql

dc.GetCommand(query).CommandText

see http://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.getcommand.aspxfor more info.

有关详细信息,请参阅http://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.getcommand.aspx

But I usually use LinqPad

但我通常使用LinqPad

回答by Sampath

There are 3 ways do that.

有 3 种方法可以做到这一点。

1.You can use LINQPad.It's Free http://www.linqpad.net/

1.您可以使用LINQPad。它是免费的http://www.linqpad.net/

2.You can use SQL Server Profiler inside the Sql Server (Tools --> SQL Server Profiler)

2.你可以在Sql Server里面使用SQL Server Profiler (Tools --> SQL Server Profiler)

3.You can use Visual Studio Debugger for Generate T-Sql.(with any visual studio version)

3.您可以使用 Visual Studio Debugger 生成 T-Sql。(适用于任何 Visual Studio 版本)

I have written a Blog Post about this check How to Convert Linq Entity Query into T-SQL ?

我写了一篇关于此检查的博客文章如何将 Linq 实体查询转换为 T-SQL ?

回答by Neil Knight

Use SQL Profilerif you are using SQL Server as your database.

使用SQL Profiler如果您正在使用SQL Server作为您的数据库。

回答by RobinJ

This popped up on Google, it's an 8-part tutorial. I think it will keep you busy for a few hours, it seems quite detailed to me.

这是在谷歌上弹出的,它是一个 8 部分的教程。我想它会让你忙几个小时,对我来说似乎很详细。

1: http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx

1:http: //weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx

2: http://weblogs.asp.net/scottgu/archive/2007/05/29/linq-to-sql-part-2-defining-our-data-model-classes.aspx

2:http: //weblogs.asp.net/scottgu/archive/2007/05/29/linq-to-sql-part-2-defining-our-data-model-classes.aspx

3: http://weblogs.asp.net/scottgu/archive/2007/06/29/linq-to-sql-part-3-querying-our-database.aspx

3:http: //weblogs.asp.net/scottgu/archive/2007/06/29/linq-to-sql-part-3-querying-our-database.aspx

4: http://weblogs.asp.net/scottgu/archive/2007/07/11/linq-to-sql-part-4-updating-our-database.aspx

4:http: //weblogs.asp.net/scottgu/archive/2007/07/11/linq-to-sql-part-4-updating-our-database.aspx

5: http://weblogs.asp.net/scottgu/archive/2007/07/16/linq-to-sql-part-5-binding-ui-using-the-asp-linqdatasource-control.aspx

5:http: //weblogs.asp.net/scottgu/archive/2007/07/16/linq-to-sql-part-5-binding-ui-using-the-asp-linqdatasource-control.aspx

6: http://weblogs.asp.net/scottgu/archive/2007/08/16/linq-to-sql-part-6-retrieving-data-using-stored-procedures.aspx

6:http: //weblogs.asp.net/scottgu/archive/2007/08/16/linq-to-sql-part-6-retrieving-data-using-stored-procedures.aspx

7: http://weblogs.asp.net/scottgu/archive/2007/08/23/linq-to-sql-part-7-updating-our-database-using-stored-procedures.aspx

7:http: //weblogs.asp.net/scottgu/archive/2007/08/23/linq-to-sql-part-7-updating-our-database-using-stored-procedures.aspx

8: http://weblogs.asp.net/scottgu/archive/2007/08/27/linq-to-sql-part-8-executing-custom-sql-expressions.aspx

8:http: //weblogs.asp.net/scottgu/archive/2007/08/27/linq-to-sql-part-8-executing-custom-sql-expressions.aspx

Good luck.

祝你好运。

回答by Conrad Frix

Another way

其它的办法

From the MSDN article How to: Display Generated SQL (LINQ to SQL)

来自 MSDN 文章How to: Display Generated SQL (LINQ to SQL)

Set the DataContext.LogProperty to Console.Outand you'll see it in the console

DataContext.Log属性设置为Console.Out,您将在控制台中看到它

回答by Mudasir Younas

The easiest way i could suggest is to go with DatabaseLog. Put Logafter initializing DataContextand you will be able to track whatever is done by EF on visual studio outputwindow.

我可以建议的最简单的方法是使用DatabaseLog. Log初始化后放置DataContext,您将能够跟踪 EF 在 Visual Studiooutput窗口上所做的任何事情。

  DataContext db = new DataContext();
  db.Database.Log = generatedSQL =>
   {
      Debug.WriteLine(generatedSQL);
   };

回答by KmKrishna

You can use Diagnostic toolsin Visual Studio to see the generated query.

可以使用Visual Studio 中的诊断工具查看生成的查询。

To enable this, go to top right corner, there you can see quick searchand type diagnostic tools.

要启用此功能,请转到右上角,您可以在那里看到快速搜索和类型诊断工具。

In the results, click on appropriate option and see below for query generated.

在结果中,单击适当的选项并查看下面生成的查询。