Dynamic Expression using LINQ. How To Find the Kitchens?

前端 未结 5 1856
难免孤独
难免孤独 2021-01-04 23:29

I try do implement a user dynamic filter, where used selects some properties, selects some operators and selects also the values.

As I didn\'t find yet an answer to

5条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-05 00:29

    // ???????????????????????? DOES NOT WORK
    var kitchens = houses.AsQueryable().Where(comparison);
    

    The Where method takes a Func or a Expression> as the parameter, but the variable comparison is of type LambdaExpression, which doesn't match. You need to use another overload of the method:

    var comparison = Expression.Lambda>(
                    Expression.Equal(houseMainRoomTypeParam,
                    Expression.Constant("Kitchen", typeof(RoomType))));
    //now the type of comparison is Expression>
    
    //the overload in Expression.cs
    public static Expression Lambda(Expression body, params ParameterExpression[] parameters);
    

提交回复
热议问题