此 LINQ-TO-SQL 查询会从表中获取所有记录吗?

发布于 2024-10-09 20:33:41 字数 596 浏览 0 评论 0原文

public long GetNewCRN()
{
   return ((from c in DataContext.GetTable<Cust_Master>() 
            select c.CUSTSERH_CRN).Max() + 1);
}

Linq to Sql 查询是否会先从表中获取所有记录,然后选择列的最大值?

如果是,那么使用 linq to sql 而不是普通的 SqlCommand 不是一个坏主意吗?

或者在 linq to sql 中还有其他方法吗?


当我附加 Console.Out 时,我什么也看不到(命令提示符甚至没有打开)。

但是当我包含以下内容时:-

context.Log = new System.IO.StreamWriter("d:\\abcd.txt");

我收到错误,“进程无法访问该文件因为它正在被另一个进程使用”,该进程是“w3wp.exe”。

那么我怎样才能看到DataContext正在执行的sql命令呢?

public long GetNewCRN()
{
   return ((from c in DataContext.GetTable<Cust_Master>() 
            select c.CUSTSERH_CRN).Max() + 1);
}

Will this Linq to Sql query fetch all records from the table first and then select the maximum of the column ?

If yes, then isn't it a bad idea using linq to sql instead of normal SqlCommand ?

Or is there any other way of doing it in linq to sql ?


When I attach Console.Out, I see nothing(command prompt does not even open).

But when I include following:-

context.Log = new System.IO.StreamWriter("d:\\abcd.txt");

I get an error, that "The process can not access the file because it is being used by another process" and that process is "w3wp.exe".

How can I see the sql commands being executed by DataContext then ?

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

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

发布评论

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

评论(1

素罗衫 2024-10-16 20:33:41

不,这不应该获取所有行。您只要求最大值。这应该转换为如下所示的 SQL 查询:

SELECT MAX(CUSTSERH_CRN) FROM Cust_Master

您可以在执行命令之前将记录器附加到 DataContext 来验证情况是否如此,例如:

DataContext.Log = Console.Out;

No this shouldn't fetch all rows. You are only requesting the maximum value. This should be converted to an SQL query like the following:

SELECT MAX(CUSTSERH_CRN) FROM Cust_Master

You can verify that this is the case by attaching a logger to the DataContext before you execute the command, for example:

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