2012-02-09 12 views
1

を組み合わせることで、私はこの方法で問題が上記の公共の方法で、私は次の行ラムダ式

(a.Starts == null) || (a.Ends == null) 

持っているということです

private Expression<Func<Auction, bool>> GetAllUnsetDatesAuctionsExpression() 
     { 
      return (a => (a.Starts == null) || (a.Ends == null)); 
     } 

このメソッドを呼び出します

public Expression<Func<Auction, bool>> GetUnsetDatesAuctionsExpression() 
     { 
      if (condition) 
       return GetAllUnsetDatesAuctionsExpression(); 
      else 
       return (a => 
         (membershipUser.ProviderUserKey != null) && 
         (a.OwnerReference == (Guid)membershipUser.ProviderUserKey) && 
         ((a.Starts == null) || (a.Ends == null))); 
      } 
     } 

を持っていますプライベートメソッドの式の本体と同じです。あなたがすることができず、ブール値と発現だから、

return (a => 
    (membershipUser.ProviderUserKey != null) && 
    (a.OwnerReference == (Guid)membershipUser.ProviderUserKey) && 
    (GetAllUnsetDatesAuctionsExpression)); 

ので、今

、これを行うには、当然のことながら、動作しません、質問は私はプライベートメソッドへの呼び出しを組み合わせないか、あります

(membershipUser.ProviderUserKey != null) && 
    (a.OwnerReference == (Guid)membershipUser.ProviderUserKey) 
+0

あなたは(a)の(GetAllUnsetDatesAuctionsExpression)を試したことがありますか? –

答えて

3

二つの組み合わせなど、新しい表現を作成し、BinaryExpression次を参照してください。

System.Linq.Expressions.BinaryExpression binaryExpression = 
    System.Linq.Expressions.Expression.MakeBinary(
    System.Linq.Expressions.ExpressionType.AndAlso, 
    firstExpression, 
    secondExpression); 
+0

問題は、私のパブリックメソッドGetUnsetDatesAuctionsExpressionがBinaryExpresssionではなくExpressionを返すということです。 –

+0

@Sachin - BinaryExpressionはまだ式です。 –

0

あなたはそれをこの式に変更することはできません:

return (a => (a.Starts == null || a.Ends == null)); 
1

を(私はスーパー取り外し以下を試してみてくださいfluous括弧):

return a => 
    membershipUser.ProviderUserKey != null && 
    a.OwnerReference == (Guid)membershipUser.ProviderUserKey && 
    GetAllUnsetDatesAuctionsExpression().Compile()(a); 
    // ---------------------------------^^^^^^^^^^^^^ 

上記のコードは、実行可能コードにあなたGetAllUnsetDatesAuctionsExpression()メソッドによって返される式ツリーによって記述ラムダ式を()コンパイル及びラムダ式を表すデリゲートを生成するExpression.Compile方法を使用します。

編集:あなたのパブリックメソッドが値ではなく式を返したことに気付かなかった。あなたのシナリオでは、もちろん、より良いのはHans Kesting's approachです。