如何使用 LINQ to SQL 处理 IN 子查询?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 23:17:51  来源:igfitidea点击:

How can you handle an IN sub-query with LINQ to SQL?

sqllinqlinq-to-sql

提问by Ian Oxley

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

我有点坚持这一点。基本上我想在 LINQ to SQL 中做类似下面的 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.

任何帮助将不胜感激。

Thanks.

谢谢。

回答by aku

General way to implement IN in LINQ to SQL

在 LINQ to SQL 中实现 IN 的一般方法

var q = from t1 in table1
        let t2s = from t2 in table2
                  where <Conditions for table2>
                  select t2.KeyField
        where t2s.Contains(t1.KeyField)
        select t1;

General way to implement EXISTS in LINQ to SQL

在 LINQ to SQL 中实现 EXISTS 的一般方法

var q = from t1 in table1
        let t2s = from t2 in table2
                  where <Conditions for table2>
                  select t2.KeyField
        where t2s.Any(t1.KeyField)
        select t1;

回答by Samuel Hyman

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:

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

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;

回答by NakedBrunch

from f in Foo
    where f.FooID ==
        (
            FROM fb in FooBar
            WHERE fb.BarID == 1000
            select fb.FooID

        )
    select f;

回答by Daren Thomas

Try using two separate steps:

尝试使用两个单独的步骤:

// create a Dictionary / Set / Collection fids first
var fids = (from fb in FooBar
            where fb.BarID = 1000
            select new { fooID = fb.FooID, barID = fb.BarID })
            .ToDictionary(x => x.fooID, x => x.barID);

from f in Foo
where fids.HasKey(f.FooId)
select f

回答by Daren Thomas

// create a Dictionary / Set / Collection fids first

// 首先创建一个字典/集合/集合

Find Other Artilces

查找其他文章

var fids = (from fb in FooBar
            where fb.BarID = 1000
            select new { fooID = fb.FooID, barID = fb.BarID })
            .ToDictionary(x => x.fooID, x => x.barID);

from f in Foo
where fids.HasKey(f.FooId)
select f

回答by Daren Thomas

// create a Dictionary / Set / Collection fids first

// 首先创建一个字典/集合/集合

Find Other Artilces

查找其他文章

var fids = (from fb in FooBar where fb.BarID = 1000 select new { fooID = fb.FooID, barID = fb.BarID }) .ToDictionary(x => x.fooID, x => x.barID);

from f in Foo where fids.HasKey(f.FooId) select f

回答by Mox Shah

from f in foo
where f.FooID equals model.FooBar.SingleOrDefault(fBar => fBar.barID = 1000).FooID
select new
{
f.Columns
};

回答by Amy B

var foos = Foo.Where<br>
( f => FooBar.Where(fb.BarId == 1000).Select(fb => fb.FooId).Contains(f.FooId));

回答by Graviton

Try this

尝试这个

var fooids = from fb in foobar where fb.BarId=1000 select fb.fooID
var ff = from f in foo where f.FooID = fooids select f