我正在嘗試爲運行時創建的表生成Lambda表達式。 的表達是建立正常,但當我打電話編譯()方法,我得到這個錯誤 「類型‘cseval.Item’的ParameterExpression不能被用於類型‘System.Object的’的委託參數」 這是我的功能動態對象的Lambda表達式
public Func<dynamic, Boolean> GetWhereExp(List<WhereCondition> SearchFieldList, dynamic item)
{
ParameterExpression pe = Expression.Parameter(item.GetType(), "c");
Expression combined = null;
if (SearchFieldList != null)
{
foreach (WhereCondition fieldItem in SearchFieldList)
{
//Expression for accessing Fields name property
Expression columnNameProperty = Expression.Property(pe, fieldItem.ColumName);
//the name constant to match
Expression columnValue = Expression.Constant(fieldItem.Value);
//the first expression: PatientantLastName = ?
Expression e1 = Expression.Equal(columnNameProperty, columnValue);
if (combined == null)
{
combined = e;
}
else
{
combined = Expression.And(combined, e);
}
}
}
var result = Expression.Lambda<Func<dynamic, bool>>(combined, pe);
return result.Compile();
}
我不相信'dynamic'被允許在'Expression's。我沒有看到代碼中的任何東西看起來像是實際上需要'動態'類型。你試過用'object'代替'dynamic'嗎? – 2014-12-13 08:51:19
是的,我已經嘗試過,但同樣的錯誤。 – Bakri 2014-12-13 08:58:05