LINQY程度更高子选择

问题描述

| 我需要使用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};