locked
like keyword using lamda expression RRS feed

  • Question

  • User-1906152199 posted
    Used the following code it gives me error as "No coercion operator is defined between types 'System.Linq.Expressions.Expression' and 'System.Boolean'." 
    I tried to convert it from expression to bool but still i struck with this. Any suggestions. 
     
     
     public Expression<Func<T, bool>> WhereLike<T>(EntityFilter filter, IEnumerable<T> data)
    
            {
                ParameterExpression parameterexpression = Expression.Parameter(typeof(T), "x");
               
    
                var body = Expression.Call(
                    typeof(StringExtensions).GetMethod("Like",
                                               BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public),
                    Expression.PropertyOrField(parameterexpression, filter.PropertyName),
                    Expression.Constant(filter.PropertyValue, typeof (string)));
                Expression conversion = Expression.Convert(body, typeof(bool));
                var lambda = Expression.Lambda<Func<T, bool>>(conversion, parameterexpression);
    
                  Func<T, bool> compiled = lambda.Compile();
                return lambda; 
    
              
            }
    
    
    
    
     internal static class StringExtensions
            {
                private static Expression Like(Expression lhs, Expression rhs)
                {
                    return Expression.Call(
                        typeof(StringExtensions).GetMethod("Like",
                                                   BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public)
                        , lhs, rhs);
                }
            }
    Thursday, February 16, 2012 3:56 PM

Answers

  • User3866881 posted

    Hummm……It seems that your Lumdba expression returns the value that is not type of boolean,you are using Like,it return you a result of found records collection。

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 22, 2012 7:50 PM

All replies

  • User-1906152199 posted

    I did exactly the link above mentioned but it says that it cannot convert expression to bool and then i tried to convert and thats when i get that error of

    "No coercion operator is defined between types 'System.Linq.Expressions.Expression' and 'System.Boolean'"

    Friday, February 17, 2012 11:49 AM
  • User3866881 posted

    "No coercion operator is defined between types 'System.Linq.Expressions.Expression' and 'System.Boolean'"

    Hello:)

    In my mind,I think because you've missed an operator that can connect the two parameters together and return a value of boolean。Operators like And,AndAlso,Or,OrElse……May all return type of boolean。

    Sample:


    // Add the following directive to your file:
    // using System.Linq.Expressions;

    // This expression perfroms a logical AND operation
    // on its two arguments, but if the first argument is false,
    // then the second arument is not evaluated.
    // Both arguments must be of the boolean type.
    Expression andAlsoExpr = Expression.AndAlso(
    Expression.Constant(false),
    Expression.Constant(true)
    );

    // Print out the expression.
    Console.WriteLine(andAlsoExpr.ToString());

    // The following statement first creates an expression tree,
    // then compiles it, and then executes it.
    Console.WriteLine(Expression.Lambda<Func<bool>>(andAlsoExpr).Compile()());

    // This code example produces the following output:
    //
    // (False AndAlso True)
    // False

    Saturday, February 18, 2012 1:57 AM
  • User-1906152199 posted
    This is what i placed. I am not clear in what are we using AndAlso for. The LHS is PropertyName and RHS is propertyvalue. So what does this statement do exactly.  
    It dint return any records which matched the input string value
    "   Expression andAlsoExpr = Expression.AndAlso(Expression.Constant(false), Expression.Constant(true)); "
            public Expression<Func<T, bool>> WhereLike2<T>(EntityFilter filter)
            {
                ParameterExpression parameterexpression = Expression.Parameter(typeof(T), "x");
    
    
                var body = Expression.Call(
                    typeof(StringExtensions).GetMethod("Like",
                                                        BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public),
                    Expression.PropertyOrField(parameterexpression, filter.PropertyName),
                    Expression.Constant(filter.PropertyValue, typeof(string)));
    
                Expression andAlsoExpr = Expression.AndAlso(Expression.Constant(false), Expression.Constant(true));
    
                var lambda = Expression.Lambda<Func<T, bool>>(andAlsoExpr, parameterexpression);
    
                Func<T, bool> compiled = lambda.Compile();
                return lambda;
    
    
            }
    
    Wednesday, February 22, 2012 1:58 PM
  • User3866881 posted

    Hummm……It seems that your Lumdba expression returns the value that is not type of boolean,you are using Like,it return you a result of found records collection。

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 22, 2012 7:50 PM
  • User-1906152199 posted

    yes finding hard time with it .....

    Thursday, February 23, 2012 10:31 AM