2016-04-06 5 views
0

ための句は、私は現在、私は一度だけ実行したいと同じ複雑なクエリを、共有する2つのquerys、持ってどこ異なる上の1つのクエリで複数のオブジェクトを選択します。問題があること、である各オブジェクト

//Query 1 
from together in (...) // ... = Complex query with multiple joins 
where together.property == 0 
select new { ... } 

//Query 2 
from together in (...) // ... = Complex query with multiple joins 
where together.property > 0 
select new { ... } 

を彼らは異なるwhere節を持っています。私は、select文のwhere句を設定しようとしましたが、これが唯一の可能性であると思われる、私はgroupbyを使用している場合、私はここに必要としない:

//Don't work 
from together in (...) // ... = Complex query with multiple joins 
select new { 
    //if together would be grouped, this would work. However I need all data without grouping 
    //   . Together is not IQueryable so this does not work 
    Foo = together.Where(e => e.property == 0).Select(...), 
    Bar = together.Where(e => e.property > 0).Select(...) 
} 

は、2つのオブジェクトが異なるに基づいて取得することが可能ですLINQを使用した1つのクエリのwhere節?

+0

ようにそれは 'DoesNotWorkException'を投げているだろうか? –

+0

'together'は' select new'の 'IQueryable'ではありません@SamIam –

+0

Query1/2は同じものを選択していますか? –

答えて

1

あなたはそれらすべてを照会し、それらを分割し、この

var qry= (
    from together in (...) // ... = Complex query with multiple joins 
    where together.property => 0 
    select together) 
    .ToList(); 

var result = new { 
    Foo = qry.Where(e => e.property == 0).Select(...), 
    Bar = qry.Where(e => e.property > 0).Select(...) 
}; 
+0

これは解決策になります、ありがとうございます。しかし、私は大量のデータを扱っています。これは翻訳されたSQLでのみ可能だと思いますか? –

+0

@ChristianGollhardtあなたがそれを構築した後、そのクエリの結果をどうするつもりですか?とにかくすべてのことを一度に照会しようとしているのであれば、このアプローチは新しいパフォーマンスの問題を導入してはならない。 –

+0

@ChristianGollhardt .Select(...)はパフォーマンスに影響する小さなサブセットのみを選択する。プロパティをチェックする前に選択することは可能ですか? –

関連する問題