런타임에 작성된 테이블에 대한 람다 식을 작성하려고합니다. 표현은 잘 구축하지만 난이 방법 컴파일()를 호출 할 때 나는 이 제 기능동적 개체에 대한 람다 식
입니다 "형 'cseval.Item'의 ParameterExpression가 '하는 System.Object'유형의 위임 매개 변수를 사용할 수 없습니다"이 오류가 당신의 방법은 기능이 아닌 표현을 반환, 그래서 이름이 'GetWhereExp은'약간 잘못 :public Func<T, Boolean> GetWhereExp<T>(List<WhereCondition> SearchFieldList, T item)
{
var pe = Expression.Parameter(item.GetType(), "c");
Expression combined = null;
if (SearchFieldList != null)
{
foreach (var fieldItem in SearchFieldList)
{
var columnNameProperty = Expression.Property(pe, fieldItem.ColumName);
var columnValue = Expression.Constant(fieldItem.Value);
var e1 = Expression.Equal(columnNameProperty, columnValue);
combined = combined == null ? e1 : Expression.And(combined, e1);
}
}
var result = Expression.Lambda<Func<T, bool>>(combined, pe);
return result.Compile();
}
작은 말 : 내가 제네릭 동적 변경 한
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'을'object'로 바꾸려고 했습니까? –
예, 시도했지만 동일한 오류가 발생했습니다. – Bakri