2009-05-04 9 views
4

私は期待に等しくなり、式を構築したいと思います...一覧<object> .Contains式ツリー

Expression<Func<ReferencedEntity, bool>> expected = (ReferencedEntity referencedEntity) => foreignKeys.Contains(referencedEntity.Id); 
Expression<Func<ReferencedEntity, bool>> actual; 

のForeignKeyタイプはここList<object>

では、これまでのところ、私が持っているものであると私は考えますExpression.Call()メソッドを使用しますが、正確にどのように行っているかはわかりません。

ParameterExpression entityParameter = Expression.Parameter(typeof(TReferencedEntity), "referencedEntity"); 
MemberExpression memberExpression = Expression.Property(entityParameter, "Id"); 
Expression convertExpression = Expression.Convert(memberExpression, typeof(object)); //This is becuase the memberExpression for Id returns a int. 

//Expression containsExpression = Expression.Call(???? 

//actual = Expression.Lambda<Func<TReferencedEntity, bool>>(????, entityParameter); 

ありがとうございます。

答えて

10

私もサミュエルの提案せずにそれを行っていることができませんでしたソリューションです...

/// <summary> 
    /// 
    /// </summary> 
    /// <param name="foreignKeys"></param> 
    /// <returns></returns> 
    private Expression<Func<TReferencedEntity, bool>> BuildForeignKeysContainsPredicate(List<object> foreignKeys, string primaryKey) 
    { 
     Expression<Func<TReferencedEntity, bool>> result = default(Expression<Func<TReferencedEntity, bool>>); 

     try 
     { 
      ParameterExpression entityParameter = Expression.Parameter(typeof(TReferencedEntity), "referencedEntity"); 
      ConstantExpression foreignKeysParameter = Expression.Constant(foreignKeys, typeof(List<object>)); 
      MemberExpression memberExpression = Expression.Property(entityParameter, primaryKey); 
      Expression convertExpression = Expression.Convert(memberExpression, typeof(object)); 
      MethodCallExpression containsExpression = Expression.Call(foreignKeysParameter 
       , "Contains", new Type[] { }, convertExpression); 

      result = Expression.Lambda<Func<TReferencedEntity, bool>>(containsExpression, entityParameter); 

     } 
     catch (Exception ex) 
     { 
      throw ex; 
     } 

     return result; 
    } 
8

解決策はわかりませんが、どうやったらそれを得ることができるのか分かります。 Expression<Func<ReferencedEntity, bool>>を受け取り、ラムダを渡すダミー関数を作成します。デバッガを使用すると、コンパイラがどのように式を作成したかを調べることができます。ここで

+0

は少しさらに私を取得...あなたの助けのおかげ – bytebender

+0

おかげで、あなたにupvoteを与えました... – bytebender