簡體   English   中英

關聯子查詢SQL與LINQ

[英]Correlated SubQuery SQL to LINQ

select * 
  from Table1 
 where TC in (select TC 
                from Table2 
               where Application in ('AAA'))`

幫助我將上述查詢轉換為LINQ。

沒有where Application in ('AAA')部分where Application in ('AAA')這看起來很簡單:

from t1 in db.Table1s
where db.Table2s.Select(t2 => t2.TC).Contains(t1.TC)
from t1 in db.Table1s

更新 (我錯了!)

List<string> myCollection = new List<string> { "AAA" };
from t1 in db.Table1s
where db.Table2s.Where(t2 => myCollection.Contains(t2.Application)).Select(t2 => t2.TC).Contains(t1.TC)
from t1 in db.Table1s

應該與代碼內集合一起使用。

嘗試這種方式

到目前為止,LINQ中沒有“ In”子查詢。

使用“任意”運算符可以完成相同的操作。

例如:

與員工位於同一城市的所有客戶

   from c in db.Customers
   where db.Employees.Any(e => e.City == c.City)
   select c;

要么

.Any()運算符的左側是子查詢。

query.Any(x => predicate)

等效於SQL

EXISTS(
    SELECT *
    FROM query
    WHERE predicate
    )

在這里獲取更多詳細信息

http://social.msdn.microsoft.com/Forums/zh-CN/linqprojectgeneral/thread/360166df-4e50-44d8-812a-04b5bc4fedd1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM