我怎样才能做一个Func<;object[],表达式<;Func<;T、 bool>>>;动态

本文关键字:gt lt Func object 表达式 动态 bool 一个 | 更新日期: 2023-09-27 18:27:10

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

基本上我想要的是:

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,这意味着它不会在数据库端执行此操作。

我怎样才能做一个Func<;object[],表达式<;Func<;T、 bool>>>;动态

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

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);
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);
}