使用 IN 运算符的 DataTable.Select

发布于 2024-09-11 10:58:32 字数 521 浏览 6 评论 0原文

我从来没有找到一种优雅的方法来做到这一点,所以我想知道其他开发人员更喜欢什么方法(为了性能、可读性等)。

有没有办法根据对另一个 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 技术交流群。

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

发布评论

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

评论(2

木緿 2024-09-18 10:58:32
from t1 in db.Table1
where (from t2 in db.Table2 where t2.SomeColumn = SomeFilterVariable select t2.SomeField).Contains(t1.MyField)
select t1.SomeValue;

如果这对您来说太混乱了:

var subQ = from t2 in db.Table2 
           where t2.SomeColumn = SomeFilterVariable 
           select t2.SomeField;

var query =  from t1 in db.Table1
             where subQ.Contains(t1.MyField)
             select t1.SomeValue;

这里最酷的事情是 LINQ 的工作方式,它将构建和构建。只执行一条SQL语句,与你贴出的SQL基本相同。

from t1 in db.Table1
where (from t2 in db.Table2 where t2.SomeColumn = SomeFilterVariable select t2.SomeField).Contains(t1.MyField)
select t1.SomeValue;

If that's too messy for you:

var subQ = from t2 in db.Table2 
           where t2.SomeColumn = SomeFilterVariable 
           select t2.SomeField;

var query =  from t1 in db.Table1
             where subQ.Contains(t1.MyField)
             select t1.SomeValue;

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.

等风来 2024-09-18 10:58:32

通过上面的例子,我认为你可以使用RIGHT JOIN来获取你想要的数据。这里:

SELECT A.SomeValue 
FROM Table1 A
RIGHT JOIN Table2 B
ON A.MyField = B.SomeField
WHERE B.SomeColumn = SomeFilterVariable

With the example above, I think you could use RIGHT JOIN to get the data you want. Here:

SELECT A.SomeValue 
FROM Table1 A
RIGHT JOIN Table2 B
ON A.MyField = B.SomeField
WHERE B.SomeColumn = SomeFilterVariable
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文