Building Dynamic GroupBy Selector Expression Tree With Multiple Properties
<p>I am to build a dynamic Expression Tree for GroupBy. All i want to achieve is like this. </p>

<pre><code>var NestedGrouped = listOfPerson.GroupByMany(x =&gt; x.Name,x=&gt;x.Age).ToList(); 
</code></pre>

<p>My Person Class is like :-</p>

<pre><code>class Person
{
 public string Name{ get; set; }
 public int Age{ get; set; }
 public float Salary{ get; set; }
}
public class GroupResult
{
        public object Key { get; set; }
        public int Count { get; set; }
        public IEnumerable Items { get; set; }
        public IEnumerable&lt;GroupResult&gt; SubGroups { get; set; }
        public override string ToString()
        { return string.Format("{0} ({1})", Key, Count); }
 }
 public static class MyEnumerableExtensions
 {
        public static IEnumerable&lt;GroupResult&gt; GroupByMany&lt;TElement&gt;(
            this IEnumerable&lt;TElement&gt; elements,
            params Func&lt;TElement, object&gt;[] groupSelectors)
        {
            if (groupSelectors.Length &gt; 0)
            {
                var selector = groupSelectors.First();

                //reduce the list recursively until zero
                var nextSelectors = groupSelectors.Skip(1).ToArray();
                return
                    elements.GroupBy(selector).Select(
                        g =&gt; new GroupResult
                        {
                            Key = g.Key,
                            Count = g.Count(),
                            Items = g,
                            SubGroups = g.GroupByMany(nextSelectors)
                        });
            }
            else
                return null;
        }
    }
</code></pre>

<p>For Single Property I am able to build the expression but i want to do GROUPBY
with multiple columns as shown above.
FOR SINGLE PROPERTY :-</p>

<pre><code> ParameterExpression parameter = Expression.Parameter(typeof(Person), "lambdaKey");
            var menuProperty = Expression.PropertyOrField(parameter, "Name");
            var lambda = Expression.Lambda&lt;Func&lt;Person, string&gt;&gt;(menuProperty, parameter);
            var selector = lambda.Compile();
            var result = P1.GroupByMany(selector);// P1 is list of PERSON
</code></pre>

<p>How to ADD multiple columns in Expression Tree (e.g (x => x.Name,x=>x.Age)).<br>
Please Help. Thanks in Advance.</p>