c# 4.0 - How can I expand(inline) property in an expression(C# 4.0)? -


i have expression of form:

expression<func<showparticipant, bool>> expr = z => z.show.orgname == "xyz"; 

i need convert/expand following form:

expression<func<showparticipant, bool>> expr = z => z.show.organization.name == "xyz"; 

where orgname property on show entity resolves organization.name. how can achieve assuming need work in ef? can imagine orgname defined in show class below -

public partial class show {     public string orgname     {                 {             return this.organization.name;         }         set         {             this.organization.name = value;         }     } } 

appreciate response,

anand.

by itself, expression trees can't there's no way expression tree know calling orgname property under covers.

however if put attribute on property, perhaps factory replace call property1 ("orgname") property path organization.name

sample code below.

    static void main(string[] args)     {         company company = new company { organization = { name = "microsoft" } };         expression<func<company, int>> lambdaexpression = c => c.orgname.length;         var expanded = expand(lambdaexpression);     }      private static expression<func<tsource, tresult>> expand<tsource, tresult>(expression<func<tsource, tresult>> lambdaexpression)     {         expression expanded = getexpandedexpression(lambdaexpression.body);         if (object.referenceequals(lambdaexpression.body, expanded))         {             return lambdaexpression;         }         return expression.lambda<func<tsource, tresult>>(             expanded,             lambdaexpression.parameters         );     }      private static expression getexpandedexpression(expression expression)     {         expression expandedcontainer;         switch (expression.nodetype)         {             case expressiontype.memberaccess:                 memberexpression memberexpression = (memberexpression)expression;                 if (memberexpression.expression != null)                 {                     expandedcontainer = getexpandedexpression(memberexpression.expression);                     propertypathattribute attribute = memberexpression.member.getcustomattributes(typeof(propertypathattribute), false).cast<propertypathattribute>().firstordefault();                     if (attribute != null && !string.isnullorempty(attribute.path))                     {                         string[] parts = attribute.path.split('.');                         expression = expandedcontainer;                         (int = 0; < parts.length; i++)                         {                             string part = parts[i];                             expression = expression.makememberaccess(                                 expression,                                 (memberinfo)expression.type.getproperty(part, bindingflags.instance | bindingflags.public | bindingflags.nonpublic) ??                                     expression.type.getfield(part, bindingflags.instance | bindingflags.public | bindingflags.nonpublic)                             );                         }                     }                     else if (!object.referenceequals(expandedcontainer, memberexpression.expression))                     {                         return expression.makememberaccess(                             expandedcontainer,                             memberexpression.member                         );                     }                 }                 break;             case expressiontype.arraylength:                 unaryexpression unaryexpression = (unaryexpression)expression;                 if (!object.referenceequals(expandedcontainer = getexpandedexpression(unaryexpression.operand), unaryexpression.operand))                 {                     return expression.arraylength(expandedcontainer);                 }                 break;         }         return expression;     }      [attributeusage(attributetargets.property | attributetargets.field, allowmultiple = false)]     public sealed class propertypathattribute : attribute     {         private readonly string _path;          public string path         {                         {                 return this._path;             }         }          public propertypathattribute(string path)         {             this._path = path;         }     }      public class organization     {         public string name { get; set; }     }      public class company     {         [propertypath("organization.name")]         public string orgname         {                         {                 return this.organization.name;             }             set             {                 this.organization.name = value;             }         }          public organization organization { get; private set; }          public company()         {             this.organization = new organization();         }     } 

Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -