5

我正在做一个动态创建 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,这意味着它不会在数据库端这样做。

4

2 回答 2

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);
于 2013-08-10T05:11:32.160 回答
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);

}
于 2013-08-10T17:03:01.940 回答