如何使用 LINQ to SQL 处理 IN 子查询? [英] How can you handle an IN sub-query with LINQ to SQL?

查看:32
本文介绍了如何使用 LINQ to SQL 处理 IN 子查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此有点困惑.基本上我想在 LINQ to SQL 中做类似下面的 SQL 查询:

I'm a bit stuck on this. Basically I want to do something like the following SQL query in LINQ to SQL:

SELECT f.* 
FROM Foo f
WHERE f.FooId IN (
    SELECT fb.FooId
    FROM FooBar fb
    WHERE fb.BarId = 1000
)

如有任何帮助,我们将不胜感激.

Any help would be gratefully received.

推荐答案

看看 这篇文章.基本上,如果你想得到IN的等价物,你需要先构造一个内部查询,然后使用Contains()方法.这是我的翻译尝试:

Have a look at this article. Basically, if you want to get the equivalent of IN, you need to construct an inner query first, and then use the Contains() method. Here's my attempt at translating:

var innerQuery = from fb in FoorBar where fb.BarId = 1000 select fb.FooId;
var result = from f in Foo where innerQuery.Contains(f.FooId) select f;

这篇关于如何使用 LINQ to SQL 处理 IN 子查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆