基于集合的执行; SQL 和 .NET

发布于 2024-09-26 03:58:21 字数 507 浏览 3 评论 0原文

在 SQL 中,人们应该始终致力于基于集合的操作而不是基于迭代(即循环)的操作。在 .NET 中,我们经常循环集合和对象。 .NET 中是否有任何命令允许基于集合的处理,或者一切都是基于迭代的? (我想起了DataAdapter.Fill如何调用DataReader来迭代结果集中的每条记录)。我对 LINQ 不太熟悉,但我的猜测是它的实现只是掩盖了幕后发生的迭代。


更新:

澄清一下:我并没有声称自己是任何天才,也不会怀疑任何使我的生活编程变得更好的才华横溢的人。我只是想问是否有命令执行基于集合的操作,就像 SQL 在 UPDATE 时所做的那样,而不是 foreach(var item in obj) { ... }显然是在遍历对象。如果 SQL 开发人员使用循环,他们就会随时受到惩罚,但在 .NET 中,我们一直在使用它们。作为一名在 SQL 和 .NET 中大量工作的开发人员,我想知道 .NET 中是否有任何替代方案可以完全避免循环。

In SQL, one should always strive for set-based operations versus iteration-based (i.e. looping). In .NET, we frequently loop collections and objects. Are there any commands in .NET that allow set-based processing or is everything iteration-based? (I'm reminded of how DataAdapter.Fill calls DataReader which iterates through each record in the result set). I'm not terribly familiar with LINQ, but my guess would be that its implementation merely masks the iterations happening behind the scenes.


UPDATE:

To clarify: I'm not claiming to be any sort of genius here and I'm not second guessing any of the brilliant people who make my life programming better. I am simply asking if there are commands that perform set-based operations, like SQL does when you UPDATE, versus foreach(var item in obj) { ... } which is clearly iterating through the object. SQL developers are chastised at every turn if ever they use a loop, yet in .NET, we use them all the time. Being a develper who works heavily in both SQL and .NET, I'm asking if there are any alternatives in .NET that avoid looping altogether.

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

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

发布评论

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

评论(2

櫻之舞 2024-10-03 03:58:21

我对 LINQ 不是很熟悉,但我的猜测是它的实现只是掩盖了幕后发生的迭代。

你认为 SQL 是如何做到的?这并不是说迭代不会发生。这取决于你如何用代码表达你的意图。基于集合和声明性的操作告诉平台你想要什么,然后让平台决定如何最好地做到这一点。它之所以有效,是因为允许这种代码的平台是其领域的专家系统,因此比人类希望的要好得多。另一方面,命令式或过程式代码准确地告诉平台要做什么以及如何做。这为机器优化留下了更少的空间,通常需要更多代码,并且更容易出现错误。

I'm not terribly familiar with LINQ, but my guess would be that its implementation merely masks the iterations happening behind the scenes.

How do you think SQL does it? It's not that iteration doesn't happen. It's a matter of how you express your intentions in code. Set-based and declarative operations tell the platform what you want, and then leave it up to the platform for figure out how best to do it. It works because the platforms that allow this kind of code are expert systems in their area, and so are much better at it than a human could hope to be. On the other hand, imperative or procedural code tells the platform exactly what to do and how to do it. This leaves less room for machine optimizations, usually requires more code, and is more prone to bugs.

对你而言 2024-10-03 03:58:21

LINQ-to-SQL 查询是真正基于集合的操作。唯一发生迭代的时间是当您直接或通过 foreach 语句调用 GetEnumerator 时。

这是可行的,因为 LINQ-to-SQL 以 IQueryable开头。 (其中 T 是充当表代理的某个类),并且应用于 IQueryable<> 的每个 LINQ 操作返回另一个 IQueryable<>。在幕后,LINQ 根本不访问数据库,而是构建越来越复杂的 LINQ 表达式。当您最终要求枚举结果时,LINQ 会获取整个表达式并将其转换为 SQL 查询,该查询作为单个请求发送到数据库。

LINQ-to-SQL queries are genuinely set-based operations. The only time iteration occurs is when you invoke GetEnumerator, either directly or via a foreach statement.

This works because LINQ-to-SQL starts with IQueryable<T> (where T is some class that acts as a surrogate for a table), and each LINQ operation you apply to an IQueryable<> returns another IQueryable<>. Behind the scenes, LINQ isn't accessing the database at all, but is instead building up a more and more complex LINQ expression. When you finally ask to enumerate the result, LINQ takes the entire expression and converts it into an SQL query that goes to database as a single request.

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