[英]More LINQY-ness (sub-select)
我需要使用 LINQ 來構建一種使用子查詢的奇怪查詢。
我真的在尋找不同的記錄。 通常,SQL 看起來像這樣:
select distinct col1, col2 from foo where col3 = somevalue
但是, col2 恰好是一個 BLOB,所以我不能使用distinct 。 所以,我認為下一個最好的 SQL 看起來像這樣:
select f1.col1, f1.col2
from foo f1 where f1.id in
(select distinct f2.id from foo f2 where f2.col3 = somevalue
我不確定在 LINQ 中“表達”第二個查詢的最佳方式是什么。 這是我到目前為止所擁有的,它有效,但我不確定它是否是最佳的:
var query = from f in foo
where f.col3 == somevalue
select new {id = f.id};
var result = from f in foo
join q in query on f.id equals q.id
select new MyType() {col1 = f.col1, col2 = f.col2};
這給了我想要的東西,但是根據 SQL Manager 的說法,生成的查詢比我手工制作的 SQL 子查詢貴 8% 左右。 有沒有更好的寫法?
你能試試這個嗎?
var result = from f in foo
where query.Contains(f.id)
select new MyType() {col1 = f.col1, col2 = f.col2};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.