c# - Expression for complex type property -
i have scenario dynamically create clause client-side grid's configuration. client sends json server parse , subsequently convert expression can passed repository clause.
for moment i'm struggling creating expressions complex property types, one:
public partial class resource { public string displayname { get; set; } public virtual resourcetype resourcetype { get; set; } }
the code below translation expression works simple types displayname property. expression like:
x => x.displayname == "valueenteredbyuserintheui"
however, when value entered in grid resourcetype property, expression like:
x => x.resourcetype == "valueenteredbyuserintheui"
i'm missing 1 step convert this:
x => x.resourcetype.name == "valueenteredbyuserintheui"
anyone can point me in right direction here?
public expression<func<t, bool>> getexpression<tentity>(string field, string operation, object value, string ignorecase) { expression<func<t, bool>> whereclause = default(expression<func<t, bool>>); // define lambda parameterexpression param = expression.parameter(typeof(t), "x"); memberexpression member = expression.property(param, field); // property type type propertytype = member.type; // converter type typeconverter converter = typedescriptor.getconverter(propertytype); // convert property type var result = converter.convertfrom(value.tostring()); // convert value constant value constantexpression constant = expression.constant(result); expression comparingexpression = default(binaryexpression); switch (operation) { case "like": comparingexpression = expression.equal(member, expression.convert(constant, member.type)); break; default: break; } var lambda = expression.lambda<func<t, bool>>(comparingexpression, param); whereclause = whereclause == default(expression<func<t, bool>>) ? lambda : newpredicatebuilder.and(whereclause, lambda); return whereclause; }
apparently not lot of people keen on expressions. anyway, have created workaround issue. added parameter in method indicating property name of complex property of root object.
// property of root object memberexpression member = expression.property(param, field); // property of property memberexpression memberfield = expression.propertyorfield(member, complexproperty);
it's not scalable nor generic now.
Comments
Post a Comment