More LINQY-ness (sub-select)
I need to use LINQ to build a kind of weird query that uses a sub-query.
I'm really looking for distinc开发者_StackOverflow中文版t records. Normally, the SQL would look like this:
select distinct col1, col2 from foo where col3 = somevalue
However, col2 just happens to be a BLOB, so I can't use distinct. So, I think that the next best SQL looks like this:
select f1.col1, f1.col2
from foo f1 where f1.id in
(select distinct f2.id from foo f2 where f2.col3 = somevalue
I'm not sure what is the best way to "phrase" that second query in LINQ. Here's what I have so far, and it works, but I'm not sure whether it's optimal:
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};
That gives me what I want, but according to SQL Manager, the resulting query is about 8% more expensive than my hand-crafted SQL sub-query. Is there a better way to write that?
Can you try this?
var result = from f in foo
where query.Contains(f.id)
select new MyType() {col1 = f.col1, col2 = f.col2};
精彩评论