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

查看:61
本文介绍了如何使用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.

谢谢.

推荐答案

看看

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天全站免登陆