55

私はこれに少し立ち往生しています。基本的に、LINQ to SQL で次の SQL クエリのようなことをしたいと考えています。

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

どんな助けでもありがたく受け取られます。

4

9 に答える 9

87

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;

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;
于 2008-09-09T08:51:32.783 に答える
65

この記事をご覧ください。基本的に、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;
于 2008-09-09T08:39:47.273 に答える
3
from f in Foo
    where f.FooID ==
        (
            FROM fb in FooBar
            WHERE fb.BarID == 1000
            select fb.FooID

        )
    select f;
于 2008-09-09T07:26:53.377 に答える
2

2 つの別々の手順を使用してみてください。

// 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
于 2008-09-09T07:25:42.083 に答える
1

// Dictionary / Set / Collection fids を最初に作成します

他の記事を探す

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
于 2009-07-06T16:58:49.437 に答える
0
var foos = Foo.Where<br>
( f => FooBar.Where(fb.BarId == 1000).Select(fb => fb.FooId).Contains(f.FooId));
于 2008-09-15T18:02:41.813 に答える
0

// Dictionary / Set / Collection fids を最初に作成します

他の記事を探す

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
于 2009-07-06T17:21:15.507 に答える
0

これを試して

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
于 2008-09-09T07:34:57.130 に答える