公开 LINQ 执行时触发的 SQL

发布于 2024-09-17 19:55:43 字数 574 浏览 1 评论 0原文

大家早上好。

为您简单介绍一下 - 在哪里可以找到 LINQ 语句触发时执行的 SQL?

我有以下代码,可以正常工作,

 var r = (from p in getproductweightsbuyer.tblWeights
                     where p.MemberId == memberid &&
                              p.LocationId == locationid
                     select p);

            if (buyer != "Not Specified")
                r = r.Where(p => p.UnitUserField1 == buyer);

            if (subcategory != "Not Specified")
                r = r.Where(p => p.UnitUserField2 == subcategory);

我只是不确定在条件 where 子句中触发的 SQL。

Morning all.

Just a quick one for you - where can I find the SQL that is executed when a LINQ statement fires?

I have the following code that works a treat,

 var r = (from p in getproductweightsbuyer.tblWeights
                     where p.MemberId == memberid &&
                              p.LocationId == locationid
                     select p);

            if (buyer != "Not Specified")
                r = r.Where(p => p.UnitUserField1 == buyer);

            if (subcategory != "Not Specified")
                r = r.Where(p => p.UnitUserField2 == subcategory);

I'm just not sure of the SQL that is firing in the conditional where clause.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

撩起发的微风 2024-09-24 19:55:43

如果您手头有数据库上下文,您可以尝试:

context.GetCommand(query).CommandText

If you have the database context at hand you could try:

context.GetCommand(query).CommandText
不必你懂 2024-09-24 19:55:43

如果您调试代码,您可以放置​​一个断点并分析 r 的值,其中将包含实际的 SQL 代码。

if you debug your code you can put a breakpoint and analyze the value of r, which will have the actual SQL code in it.

岁月无声 2024-09-24 19:55:43

除了上述选项之外,您还可以运行 SQL 分析器并查看通过线路发送到数据库的实际 SQL。

As well as the above options, you can also run up SQL profiler and see the actual SQL sent down the wire to the DB.

垂暮老矣 2024-09-24 19:55:43

如果您使用 LINQ to SQL,则可以设置 DataContext.Log 属性。当您执行查询时,这将记录 SQL:

getproductweightsbuyer.Log = Console.Out;

var r = (from p in getproductweightsbuyer.tblWeights
                    where p.MemberId == memberid &&
                             p.LocationId == locationid
                    select p);

           if (buyer != "Not Specified")
               r = r.Where(p => p.UnitUserField1 == buyer);

           if (subcategory != "Not Specified")
               r = r.Where(p => p.UnitUserField2 == subcategory);

foreach (var row in r)
  ...

If you use LINQ to SQL you can set the DataContext.Log property. This will log the SQL when you execute the query:

getproductweightsbuyer.Log = Console.Out;

var r = (from p in getproductweightsbuyer.tblWeights
                    where p.MemberId == memberid &&
                             p.LocationId == locationid
                    select p);

           if (buyer != "Not Specified")
               r = r.Where(p => p.UnitUserField1 == buyer);

           if (subcategory != "Not Specified")
               r = r.Where(p => p.UnitUserField2 == subcategory);

foreach (var row in r)
  ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文