我怎么能做一个Func <object [],表达式<func <t,bool>&gt;&gt;动态?</对象[],表达<FUNC <t时,布尔>

时间:2013-08-09 23:19:03

标签: c# .net lambda

我正在做一个动态创建lambda函数来查询数据库的.Find(objects [] keys)方法。

基本上我想要的是:

var mykeys = new Guid("37ec1659-b35e-46c9-a7fc-e9802644ca1a");
IQueryable<T> database ;
Func<object[],Expression<Func<T,bool>>> objectFinder = CreateKeyExpression(typeof(T));
var foundObject = database.FirstOrDefault(objectFinder(mykeys));

private LambdaExpression CreateKeyExpression(Type C1Type)
{

    ParameterExpression instanceParameter = Expression.Parameter(C1Type);
    ParameterExpression keyParameters = Expression.Parameter(typeof(object[]));

    PropertyInfo[] objectKeys = C1Type.GetKeyProperties().ToArray();
    var expr = Expression.Equal( Expression.TypeAs( Expression.Property(instanceParameter,objectKeys[0]),typeof(object)),
      Expression.ArrayIndex(keyParameters,Expression.Constant(0))); 


    for (int i = 1; i < objectKeys.Length; i++)
    {
        expr = Expression.AndAlso(expr, Expression.Equal(
            Expression.Property(instanceParameter,objectKeys[i]),
            Expression.ArrayIndex(keyParameters,Expression.Constant(i))
            )); 

    }

    var lmp= Expression.Lambda(expr, instanceParameter);

    return Expression.Lambda(lmp, keyParameters);
}

关于如何实现这一目标的任何想法?上面给了我一个Func<object[],Func<T,bool>>,它使IQueryable成为IEnumerable,这意味着它不会在数据库端完成它。

2 个答案:

答案 0 :(得分:2)

您需要使用Expression.Constant方法而不是Expression.ArrayIndex,因为您无法使用FirstOrDefault方法使用键值传递到表达式数组。

private static LambdaExpression CreateKeyExpression(Type C1Type, object[] parameters)
{
    var instanceParameter = Expression.Parameter(C1Type);
    PropertyInfo[] objectKeys = C1Type.GetKeyProperties().ToArray();

    var expr = Expression.Equal(Expression.Property(instanceParameter, objectKeys[0]),
        Expression.Constant(parameters[0], objectKeys[0].PropertyType));


    for (int i = 1; i < objectKeys.Length; i++)
    {
        expr = Expression.AndAlso(expr, Expression.Equal(
            Expression.Property(instanceParameter, objectKeys[i]),
            Expression.Constant(parameters[i], objectKeys[i].PropertyType)));
    }
    return Expression.Lambda(expr, instanceParameter);
}

var parameters = new object[] { Guid.NewGuid(), Guid.NewGuid() };
var lambdaExpression = CreateKeyExpression(typeof(TestClass), parameters);
var testClasses = new List<TestClass>() { new TestClass { Id = (Guid)parameters[0], Id1 = (Guid)parameters[1] } };
var testClass = testClasses.AsQueryable().FirstOrDefault((Expression<Func<TestClass, bool>>)lambdaExpression);

答案 1 :(得分:0)

private Expression<Func<object[], Expression<Func<C1Source, bool>>>> CreateKeyExpression<C1Source>()
{

    ParameterExpression instanceParameter = Expression.Parameter(typeof(C1Source));
    ParameterExpression keyParameters = Expression.Parameter(typeof(object[]));

    PropertyInfo[] objectKeys = typeof(C1Source).GetKeyProperties().ToArray();
    var expr = Expression.Equal( Expression.Property(instanceParameter,objectKeys[0]),
     Expression.Convert( 
        Expression.ArrayIndex(keyParameters,Expression.Constant(0)),
        objectKeys[0].PropertyType)
        ); 

    for (int i = 1; i < objectKeys.Length; i++)
    {
        expr = Expression.AndAlso(expr, Expression.Equal(
            Expression.Property(instanceParameter,objectKeys[i]),
            Expression.Convert(
                Expression.ArrayIndex(keyParameters,Expression.Constant(i)),
                objectKeys[i].PropertyType)
                ); 

    }

    var lmp= Expression.Lambda(expr, instanceParameter);

    return Expression.Lambda<Func<object[], Expression<Func<C1Source, bool>>>>(lmp, keyParameters);

}