私は、エンティティに対して動的なlinqクエリを使用するプロジェクトに取り組んでいます。私は膨大な量のケースを抱えており、コードの重複を避けるためにメソッドにリファクタリングしています。ただし、ストア式にないメソッドを使用すると、例外がスローされます。解決策の1つは、メソッドの結果を、linqで解釈してクエリを開始できる式にカプセル化することです。
そのコードを考えてみましょう:
parentExpression = x => x.child.Any(y=>IsGoodChild(y,childType, childSize));
private bool IsGoodChild(child c, int childType, int childSize){
return c.type == childType && c.size == childSize;
}
「parentExpression」は、私のEFの「Parent」タイプの述語です。このコードは例外をスローします。「IsGoodChild」メソッドはブール値を返し、linqによってエンティティに解釈できません。
だから、私はこのようなものが欲しいです:
parentExpression = x => x.child.AsQueryable().Any(IsGoodChild(childType, childSize));
private System.Linq.Expression.Expression<Func<child, bool>> IsGoodChild(int childType, int childSize){
return ????
}
では、x.child属性を受け取らなくても、「IsGoodChild(...)」はどのように機能するのでしょうか。事前のThx
再、
このような式でラムダを直接書くとき、私は何かを試みます:
parentExpression = x => x.child.Any(y=>y.type == childType && y.size == childSize);
私はresharperからextractメソッドを使用し、これを生成しました:
private Expression<Func<child,Boolean>> IsGoodChildFunctional(Int32 childType, Int32 childSize)
{
return c => c.type == childType && c.size == childSize;
}
しかし、.NETFrameworkデータプロバイダーエラー1025'エラーもあります...