Note that there are some explanatory texts on larger screens.

plurals
  1. PONested Lambda for use with Fluent Interface
    text
    copied!<p><strong>Given the following types:</strong></p> <pre><code>class Parent { List&lt;Child&gt; Children {get;set;}} class Child {List&lt;Child&gt; GrandChildren {get;set;}} class Helper&lt;TEntity&gt; {List&lt;string&gt; Properties {get;set;}} </code></pre> <p><strong>And given the following methods on Helper...</strong></p> <pre><code>public Helper AddProps&lt;TEntity, TProp&gt;(Expression&lt;Func&lt;TEntity, TProp&gt;&gt; exp) { this.Properties.Add(GetPropInfo(exp).Name); } public PropertyInfo GetPropInfo(Expression&lt;Func&lt;TEntity, TProp&gt;&gt; exp) { return (PropertyInfo)((MemberExpression)(expression.Body)).Member; } </code></pre> <p><strong>I am able to do this:</strong></p> <pre><code>Helper&lt;Parent&gt; myHelper = new Helper&lt;Parent&gt;(); myHelper.AddProps(x =&gt; x.Children); </code></pre> <p>The string list "Properties" on myHelper would then contain the value "Children", the name of the property passed through the expression.</p> <p>What I want to do now is to be able to achieve the same thing, only with the ability to reflect type hierarchy.</p> <p><strong>Would it look like this ?</strong></p> <pre><code>x =&gt; x.Children { xx =&gt; xx.GrandChildren } </code></pre> <p>Or is it even possible, and what would be involved? I've seen nested lambda's before but don't know what's involved.</p> <p>Thanks in advance!</p> <p><strong>EDIT</strong></p> <p>It seems there is some confusion so I'll try to clarify. I want to be able to create a string that looks like this "Object.SubObject.SubSubObject" using lambda expressions and method chaining. My example does this, but only for one level deep ( a property of a class). What I want to do is extend this to go to any depth.</p> <p>For Example, I'd like to use lambda expressions with a fluent interface that would look something like this....</p> <p>AddProps(x => x.Children).AddProps(xx => xx.GrandChildren) and that would add "Children.GrandChildren" to my "Properties" string list.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload