我需要使用表达式树搜索忽略字符串大小写的字符串

I need to search strings ignoring case of the strings using Expression Trees

我想构建一个表达式树,它的行为就像在 SQL 中一样。也就是说,我希望它忽略字符串的大小写,例如:

"ThIs Is A tEsT""tHiS iS a TeSt""this is a test""THIS IS A TEST" 都应匹配到 "this is a test".

public class Datum
{
    public string Value { get; set; }
}

void Main()
{
    var data = new List<Datum> {
        new Datum { Value = "ThIs Is A tEsT" },
        new Datum { Value = "tHiS iS a TeSt" },
        new Datum { Value = "this is a test" },
        new Datum { Value = "THIS IS A TEST" }
    };

    var queryableData = data.AsQueryable<Datum>();
    var pe = Expression.Parameter(typeof(Datum), "Value");
    var right = Expression.Constant("this is a test", typeof(string));
    var e = Expression.Equal(Expression.Property(pe, "Value"), right);
    var whereCallExpression = Expression.Call(
        typeof(Queryable),
        "Where",
        new Type[] { queryableData.ElementType },
        queryableData.Expression,
        Expression.Lambda<Func<Datum, bool>>(e, pe));   
    var results = queryableData.Provider.CreateQuery<Datum>(whereCallExpression);
    results.Dump();
}

我尝试了几种方法,但似乎没有任何效果。

我发现完成此任务的最直接方法是像这样调用自定义方法:

public class Datum
{
    public string Value { get; set; }
}

void Main()
{
    var data = new List<Datum> {
        new Datum { Value = "ThIs Is A tEsT" },
        new Datum { Value = "tHiS iS a TeSt" },
        new Datum { Value = "this is a test" },
        new Datum { Value = "THIS IS A TEST" }
    };

    var queryableData = data.AsQueryable<Datum>();
    var pe = Expression.Parameter(typeof(Datum), "Value");
    var right = Expression.Constant("this is a test", typeof(string));

    // * First build a func that will use the method you want.  In this case, it is going to be any equals that ignores case.
    // * Next Invoke that method instead of Expression.Equal.
    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    Expression<Func<string, string, bool>> IgnoreCase = (op1, op2) => op1.Equals(op2, StringComparison.OrdinalIgnoreCase);
    var e = Expression.Invoke(IgnoreCase, Expression.Property(pe, "Value"), right);

    //var e = Expression.Equal(Expression.Property(pe, "Value"), right);
    var whereCallExpression = Expression.Call(
        typeof(Queryable),
        "Where",
        new Type[] { queryableData.ElementType },
        queryableData.Expression,
        Expression.Lambda<Func<Datum, bool>>(e, pe));   
    var results = queryableData.Provider.CreateQuery<Datum>(whereCallExpression);
    results.Dump();
}