c# - Returning Expression<> using various class properties -
i have 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 can see done on message.body
i same thing on other string properties on message
class , don't want duplicate code.
is there way passing in property somehow?
you can try composing expression manually, give ability specify property name string. isn't complete solution , it's untested, may give starting point see mean:
var parameter = expression.parameter(typeof(message), "o"); var getname = expression.property(parameter, "body"); var isnullorempty = expression.not(expression.call(typeof(string), "isnullorempty", null, getname)); var compare = expression.equal(getname, expression.constant("thisvalue")); var combined = expression.and(isnullorempty, compare); var lambda = expression.lambda(combined, parameter);
so change function accept "body" parameter, , cast lambda @ end to:
expression<func<message, bool>>
i may not have got syntax creating lambda right though.
Comments
Post a Comment