Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing Expression ToLower on List throws exception

I have the below code:

public Expression FilterString(string property, string value, ParameterExpression parameter)
{
    //Create Message MemberExpression from parameter and properted eg/Message.Body
    var getname = Expression.Property(parameter, property);

    //Create expression for Not IsNullOrEmpty
    var isnullorempty = Expression.Not(Expression.Call(typeof(string), "IsNullOrEmpty", null, getname));

    //Create expression for member property equal to value eg/Message.Body = "hi"
    var toLower = Expression.Call(getname,
                         typeof(string).GetMethod("ToLower", System.Type.EmptyTypes));
    var compare = Expression.Equal(toLower, Expression.Constant(value));

    //Create expression combining Not IsNullOrEmpty AND Equal To
    var condition = Expression.And(isnullorempty, compare);

    return condition;
}

If I run the below test I get a NullReferenceException, I assume because some of the items have a null body but thats exactly what the Expression should be testing.

private IEnumerable<Message> GetMessages()
{
    var list = new List<Message>();

    var message = new Message() { Body = null, Properties = new Collection<MessageProperty>() };
    list.Add(message);

    message = new Message() { Body = "", Properties = new Collection<MessageProperty>() };
    list.Add(message);

    message = new Message() { Body = "Hello Everybody", Properties = new Collection<MessageProperty>() };
    list.Add(message);

    message = new Message() { Flag = 1, Properties = new Collection<MessageProperty>() };
    list.Add(message);

    message = new Message() { ChannelInt = 1,  Properties = new Collection<MessageProperty>() };
    list.Add(message);

    message = new Message() { Properties = new Collection<MessageProperty>(new[] { new MessageProperty() { Key = "Gender", Value = "male" } }) };
    list.Add(message);

    return list;
}

[Theory]
[InlineData("Hello Everybody")]
[InlineData("hello everybody")]
public void FilterString_NullAndEmptyMessages_Removed(string searchTerm)
{
    var messageList = GetMessages();

    var parameter = Expression.Parameter(typeof(Message), "message");

    var equalToFilterType = new EqualToFilterType();

    var filter = equalToFilterType.FilterString("Body", searchTerm, parameter);

    var lambda = Expression.Lambda(filter, parameter);

    //******EXCEPTION*******//
    var result = messageList.AsQueryable().Where((Expression<Func<Message, bool>>)lambda).ToList();

    Assert.Equal(1, result.Count);
}

Any ideas how I can get this test to pass? (My expression code will be used to hit SQL Server too BTW)

like image 647
Jon Avatar asked Sep 25 '26 22:09

Jon


1 Answers

I suspect this is the problem:

var condition = Expression.And(isnullorempty, compare);

I suspect you want AndAlso:

var condition = Expression.AndAlso(isnullorempty, compare);

Effectively, at the moment you've got

x & y

whereas you want

x && y
like image 115
Jon Skeet Avatar answered Sep 28 '26 12:09

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!