2013-04-11 65 views
0

試圖創建一個使用表達式目錄樹下面的表達式(想幫助)不能建立lambda表達式樹

List<string> lstName = dt_Name.Select(y => y.Name); 

List<string> lstLabName = dt_Label.Select(x => x.LabelName).Where(p => p.LabelName.StartsWith(lstName + '_')); 

我想它確實去了所有lstName並找到lstLabName所有實例是符合條件。

我想出了到目前爲止的代碼是:

private BinaryExpression CreateBinaryExpression(string buildName) 
    { 
     // x.LabelName 
     var param = Expression.Parameter(typeof(dt_Label), "x"); 
     var key = param.GetType().GetProperty("LabelName"); 
     var left = Expression.MakeMemberAccess(param, key); 

     //ParameterExpression left = Expression.Parameter(typeof(string), "p"); //p 
     ConstantExpression constantExpression = Expression.Constant(buildName, typeof(string)); 

     // x.LabelName == buildName 
     return Expression.MakeBinary(ExpressionType.Equal, left, constantExpression); 
    } 

    private BinaryExpression CalculateLambdaExp(List<string> lstBuildName) 
    { 
      BinaryExpression binaryExpression = CreateBinaryExpression(lstBuildName[0]); 

      if (lstBuildName.Count() > 1) 
      { 
       List<string> lstnStr = lstBuildName; 
       lstnStr.RemoveAt(0); 
       BinaryExpression calculatedLambdaExp = CalculateLambdaExp(lstnStr); 
       binaryExpression = Expression.MakeBinary(ExpressionType.AndAlso, binaryExpression, calculatedLambdaExp); 
      } 

      return binaryExpression; 
    } 

    private List<string> RunLambdaExpression(List<string> BuildName) 
    { 
     ParameterExpression left = Expression.Parameter(typeof(string), "p"); //p 

     var factorial = Expression.Lambda<Func<List<string>, List<string>>>(CalculateLambdaExp(BuildName), left); 
     var n = factorial.Compile()(BuildName); 
     List<string> lst = n.ToList(); 

     return lst; 
    } 

我獲得充足的運行時錯誤。將不勝感激任何幫助。

想通大部分出來:

改變了功能CreateBinaryExpression到:

private Expression CreateBinaryExpression(string buildName) 
    { 
     // x.LabelName 
     var param = Expression.Parameter(typeof(dt_Label), "x"); 
     var key = typeof(dt_Label).GetProperty("LabelName"); 

     var left = Expression.MakeMemberAccess(param, key); 

     ConstantExpression constantExpression = Expression.Constant(buildName + '_', typeof(string)); 

     //x.LabelName.startsWith(buildName_) 
     MethodInfo mi = typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) }); 
     Expression call = Expression.Call(left, mi, constantExpression); 

     return call; 
    }; 

,改變所有引用函數接收表達,而不是BinaryExpression

+2

什麼運行時錯誤? – Alxandr 2013-04-11 17:43:34

+0

沒有進一步觀察,我可以看到'param.GetType()。GetProperty(「LabelName」);'是錯誤的 - 您可能希望'typeof(dt_label)'在這裏。 – 2013-04-11 17:46:59

+1

爲什麼你需要表達式樹? – svick 2013-04-11 18:41:48

回答

3

這條線是一個問題

var key = param.GetType().GetProperty("LabelName"); 

param.GetType()將返回System.Linq.Expressions.ParameterExpression,它沒有LabelName屬性。

您應該使用typeof(dt_Label).GetProperty("LabelName")

+0

上失敗,而不是:var key = param.GetType()。GetProperty(「LabelName」);我試過了:var key = param.Type.GetField(「LabelName」);但關鍵字爲空,爲什麼? – aura 2013-04-11 20:51:04

+0

和我想要得到的是:dt_Label.LabelName – aura 2013-04-11 20:53:01

+0

然後嘗試'typeof(dt_Label).GetProperty(「LabelName」)'。我可能錯誤'param.Type' – 2013-04-11 21:05:02