C# 更多细节(子选择)
我需要使用LINQ来构建一种使用子查询的奇怪查询 我真的在寻找不同的记录。通常,SQL将如下所示:C# 更多细节(子选择),c#,linq,entity-framework,C#,Linq,Entity Framework,我需要使用LINQ来构建一种使用子查询的奇怪查询 我真的在寻找不同的记录。通常,SQL将如下所示: select distinct col1, col2 from foo where col3 = somevalue select f1.col1, f1.col2 from foo f1 where f1.id in (select distinct f2.id from foo f2 where f2.col3 = somevalue 然而,col2恰好是一个BLOB,所以我不能
select distinct col1, col2 from foo where col3 = somevalue
select f1.col1, f1.col2
from foo f1 where f1.id in
(select distinct f2.id from foo f2 where f2.col3 = somevalue
然而,col2恰好是一个BLOB,所以我不能使用distinct。因此,我认为下一个最好的SQL如下所示:
select distinct col1, col2 from foo where col3 = somevalue
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};
你能试试这个吗
var result = from f in foo
where query.Contains(f.id)
select new MyType() {col1 = f.col1, col2 = f.col2};
听起来你想做点什么,但你没有意识到你可以做,所以你问了一个关于其他事情的问题-你是否试图问你如何选择col1和col2,但只运行与col1不同的程序?这是可能的。@Nick:是的,那会解决问题。听起来你想做点什么,但你没有意识到你可以做,所以你问了一个关于其他事情的问题-你是想问你如何选择col1和col2,但只运行与col1不同的程序吗?那是可能的。@Nick:是的,那会解决问题。你的是我发现的最有效的选择。谢谢你的是我发现的最有效的选择。谢谢