Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning Expression<> using various class properties

Tags:

c#

.net

lambda

linq

I have something like this:

public Expression<Func<Message, bool>> FilterData()
{
    switch (this.operatorEnum)
    {
        case FilterParameterOperatorEnum.EqualTo:
            return message => !string.IsNullOrEmpty(message.Body) &&
                              message.Body
                                     .Equals(this.value, StringComparison.InvariantCultureIgnoreCase);

        case FilterParameterOperatorEnum.NotEqualTo:

            return message => !string.IsNullOrEmpty(message.Body) &&
                              !message.Body
                                     .Equals(this.value, StringComparison.InvariantCultureIgnoreCase);

        case FilterParameterOperatorEnum.Contains:

            return message =>
                    !string.IsNullOrEmpty(message.Body) &&
                    message.Body.IndexOf(this.value,
                                   StringComparison.InvariantCultureIgnoreCase) >= 0;

        case FilterParameterOperatorEnum.DoesNotContain:
            return message =>
                    !string.IsNullOrEmpty(message.Body) &&
                    message.Body.IndexOf(this.value,
                                   StringComparison.InvariantCultureIgnoreCase) == -1;


    }
}

As you can see this is done on Message.Body

I now what to do the same thing on other string properties on the Message class and I don't want to duplicate all that code.

Is there a way to do that by passing in the property somehow?

like image 565
Jon Avatar asked May 21 '13 09:05

Jon


People also ask

What is a class property in C#?

Properties enable a class to expose a public way of getting and setting values, while hiding implementation or verification code. A get property accessor is used to return the property value, and a set property accessor is used to assign a new value.

Which operator can you use to code an expression-bodied property or method?

The expression-bodied syntax can be used when a member's body consists only of one expression. It uses the =>(fat arrow) operator to define the body of the method or property and allows getting rid of curly braces and the return keyword. The feature was first introduced in C# 6.

Can we have private properties in C#?

Properties can be marked as public , private , protected , internal , protected internal , or private protected . These access modifiers define how users of the class can access the property. The get and set accessors for the same property may have different access modifiers.


1 Answers

Eclude the expression that retrieves the property value into a separate lambda expression:

public Expression<Func<Message, bool>> FilterData(Func<Message, string> retrievePropValueFunc)

In your filter expressions, you can then call that new lambda expression (just showing one as an example):

return message => !string.IsNullOrEmpty(retrievePropValueFunc(message))
        && retrievePropValueFunc(message)
                .Equals(this.value, StringComparison.InvariantCultureIgnoreCase);

To get to the Body property, pass message => message.Body to the retrievePropValueFunc parameter; as you see, you can modify this to pass different lambda expressions for the retrieval of other properties just as well.

like image 145
O. R. Mapper Avatar answered Oct 06 '22 20:10

O. R. Mapper