使用 IN 运算符的 DataTable.Select
我从来没有找到一种优雅的方法来做到这一点,所以我想知道其他开发人员更喜欢什么方法(为了性能、可读性等)。
有没有办法根据对另一个 DataTable 的查询结果在 DataTable.Select() 函数中使用 LIKE 运算符。例如,在 SQL Server 中,语法为:
Select SomeValue
From Table1
WHERE MyField IN
(Select SomeField From Table2 Where SomeColumn = SomeFilterVariable)
我知道从编码的角度来看,这是针对数据库执行的非常简单的查询,但这是在应用程序中完成的,这样做会导致数百个由于涉及的计算数量,对服务器进行了数千次调用。我已经对其进行了计时,最好立即获取所有数据并使用 DataTable.Select() 或 DataTable.Compute() 来获取我需要的结果。
我对任何事情都持开放态度,包括 LINQ to datasets 等。我真正需要避免的是多次访问服务器。
I've never found an elegant way to do this, so I'm wondering what methods other developers prefer (for performance, readability, etc).
Is there a way to use the LIKE operator in the DataTable.Select() function, based on the results of a query to another DataTable. For example, in SQL Server, the syntax would be:
Select SomeValue
From Table1
WHERE MyField IN
(Select SomeField From Table2 Where SomeColumn = SomeFilterVariable)
I know that from a coding standpoint, this is a very simple query to do against the DB, but this is being done in an app where doing it this way would result in hundreds of thousands of calls to a server due to the number of calculations involved. I've timed it, and it's much better to go get all the data at once and use DataTable.Select() or DataTable.Compute() to get the results I need.
I'm open to anything including LINQ to datasets, etc. What I really need to avoid is a lot of trips to the server.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果这对您来说太混乱了:
这里最酷的事情是 LINQ 的工作方式,它将构建和构建。只执行一条SQL语句,与你贴出的SQL基本相同。
If that's too messy for you:
The cool thing here is the way LINQ works, that will build & execute only one SQL statement, basically the same as the SQL you posted.
通过上面的例子,我认为你可以使用
RIGHT JOIN
来获取你想要的数据。这里:With the example above, I think you could use
RIGHT JOIN
to get the data you want. Here: