小编Wou*_*ter的帖子

使用表达式树创建完全动态的where子句并在IQueryable上执行

在我的代码中的第(3)点,我定义了一个名为query1的查询,其中我定义了一个.Where lambda表达式.此查询在某种程度上是动态的,但仍包含静态元素,它始终引用Type Employee及其(int)属性ClientID.

现在我非常希望根据方法参数来引用类型及其属性动态,例如,如下面的点(1)所示.

到目前为止我尝试的是通过将(例如)(4),(5)和(6)中记载的更复杂的表达式树替换为使得在点(3)下定义的查询的静态部分完全动态化.但是,当我尝试将所有内容添加到一起时,它说我打电话.参数错误.我不知道如何调用.使用正确的参数来创建一个完全动态的选择.

有人知道解决这个问题吗?我花了一天时间搜索,到目前为止还没有找到解决方案.

        dsMain domainService = new dsMain();


        //(1)i want to rewrite the following four variables to method-parameters
        Type entityType = typeof(Employee);
        String targetProperty = "ClientID";
        Type entityProperty = typeof(Employee).GetProperty(targetProperty).PropertyType;
        int idToDelete = 5;


        //(2)create expression-function: idToDelete == entityType.targetProperty (in this case: Employee.ClientID)
        ParameterExpression numParam = Expression.Parameter(entityProperty, targetProperty.Substring(0, 3));
        ConstantExpression equalTarget = Expression.Constant(idToDelete, idToDelete.GetType());
        BinaryExpression intEqualsID = Expression.Equal(numParam, equalTarget);
        Expression<Func<int, bool>> lambda1 =
                    Expression.Lambda<Func<int, bool>>(
                    intEqualsID,
                    new ParameterExpression[] { numParam });

        //(3)I want to create query1 fully …
Run Code Online (Sandbox Code Playgroud)

c# lambda iqueryable where expression-trees

7
推荐指数
1
解决办法
9438
查看次数

为什么java推断失败

我有以下看似相似的方法,do1并且do2:

class Demo<A>{
    public <C> Iterable<C> do1(List<? super C> _a) {
        return null;
    }


    public <C extends D, D> Iterable<C> do2(List<D> _a) {
        return null;
    }

    {
        List<? extends A> leA = null;

        do2(leA);
        do1(leA);
    }
}
Run Code Online (Sandbox Code Playgroud)

当我编译上面的代码(javac的1.8.0_92),呼吁do2(leA)作品而do1(leA)失败.

required: List<? super C>
found: List<CAP#1>
reason: cannot infer type-variable(s) C
(argument mismatch; List<CAP#1> cannot be converted to List<? super C>)
where C,A are type-variables:
  C extends Object declared in method <C>do1(List<? super C>) …
Run Code Online (Sandbox Code Playgroud)

java type-inference javac

6
推荐指数
1
解决办法
115
查看次数

标签 统计

c# ×1

expression-trees ×1

iqueryable ×1

java ×1

javac ×1

lambda ×1

type-inference ×1

where ×1