简体   繁体   中英

Case-insensitive Func

I have a Func<Message, bool> where Message is a class of mine.

I build up a Lambda and compile it into a Func<Message,bool>

public static Func<Classes.Message, bool> GetPredicate(string expression)
{
    Func<Classes.Message, bool> result = null;

    try
    {
        ParameterExpression parameter = Expression.Parameter(typeof(Classes.Message), "Message");
        var lambda = DynamicExpression.ParseLambda(new[] { parameter }, null, expression);
        result = lambda.Compile() as Func<Classes.Message, bool>;
    }
    catch (Exception e)
    {
        _log.Fatal(e);
    }

    return result;
}

This results in

Message => (((Message.ContainsProperty("Gender") == True) AndAlso (Message.GetPropertyValue("Gender") != "Female")) AndAlso (Message.ChannelString != "FacebookComment"))

Later on, the Func gets executed but I'd like to make it case-insensitve so when a Message objet gets passed in but the gender is "female" not "Female" it still returns a true in the bool.

Is that possible?

Thanks

使用ToUpper()

Message.GetPropertyValue("Gender").ToUpper() != "FEMALE"

That's being generated from the expression.

I'm fairly certain that the static public methods on string are exposed via DynamicQuery, so you would need to you string.compare:

(x) => String.Compare (x.Gender, "Female", StringComparison.OrdinalIgnoreCase)

Unfortunately this would mean that you can't use the sql-like syntax, but it should parse and run.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM