7

次の形式の Linq to Entities クエリがあります。

var x = from a in SomeData
    where ... some conditions ...
    select new MyType
    {
        Property = a.Property,
        ChildCollection = from b in a.Children
                        select new MyChildType
                        {
                            SomeProperty = b.Property,
                            AnotherProperty = b.AnotherProperty
                        }
    };

var y = from a in SomeData
    where ... some other conditions ...
    select new MyType
    {
        Property = a.Property,
        ChildCollection = from b in a.Children
                        select new MyChildType
                        {
                            SomeProperty = b.Property,
                            AnotherProperty = b.AnotherProperty
                        }
    };

var results = x.Concat(y);

(これは単純化された例です。'where' 句と 'select' 句は、ここに示されているよりも複雑です。単一の結合されたものを作成するのは複雑すぎて、条件が多すぎて時間がかかるため、個別のクエリ ステートメントを使用しています。コンパイル)

正常にコンパイルされますが、実行時に例外が発生して失敗します。

"The nested query is not supported. Operation1='UnionAll' Operation2='MultiStreamNest'

注、ネストされた型付き構造に投影しようとしています。Concat() の前に x と y で .ToList() を呼び出すと、正常に動作します。さらなるポイントとして、私のプロパティの 1 つは列挙型ですが、整数ラッパー プロパティを使用してそれに割り当てています。

すべてのデータをメモリに取り込まずにやりたいことを実行できる方法はありますか? それとも列挙型が失敗の原因ですか?

ありがとう、

T

4

3 に答える 3

0

ナビゲーションプロパティの複数のセットを単一のIEnumerableに連結または結合しようとしているときに、同様の問題が発生しました。コードサンプルは次のとおりです。

var requiredDocuments =                
                 (from x in db.RequestTypes where (some condition) select x.RequiredDocuments)
                 .SelectMany(r => r).ToList<DataModel.RequiredDocument>()
                 .Concat(
                 (from c in db.Categories where (some condition) select c.RequiredDocuments)
                 .SelectMany(r => r).ToList<DataModel.RequiredDocument>()
                 )
                 .Concat(
                 (from f in db.Fields where (some condition) select f.RequiredDocuments)
                 .SelectMany(r => r).ToList<DataModel.RequiredDocument>()
                );
于 2013-10-21T11:09:50.820 に答える
0

あなたが何をしようとしているのかを正しく理解していれば、同じ問題に何度か遭遇しました。肝心なのは、ネストされたプロジェクションを使用したユニオンの実行はサポートされていないということです。それを行う必要がある場合は、最初に ToList で結果を具体化する必要があります。

于 2017-03-02T21:49:31.903 に答える
0

試してみましたか

 var results = x.Union(y);

?

ティズ

また

var x = (from a in SomeData
where ... some conditions ...
select new MyType
{
    Property = a.Property,
    ChildCollection = (from b in a.Children
                    select new MyChildType
                    {
                        SomeProperty = b.Property,
                        AnotherProperty = b.AnotherProperty
                    }).ToArray() //or DefaultIfEmpty
}).Concat(
from a in SomeData
where ... some other conditions ...
select new MyType
{
    Property = a.Property,
    ChildCollection = (from b in a.Children
                    select new MyChildType
                    {
                        SomeProperty = b.Property,
                        AnotherProperty = b.AnotherProperty
                    }).ToArray() //or DefaultIfEmpty
});
于 2012-04-13T09:07:19.830 に答える