title: MongoDb C# driver 2.4 - LINQ with dynamic GroupBy not supported
body: <pre><code>var result = await Events.AsQueryable()
              .Where(_ =&gt; _.Date &gt;= from.Date &amp;&amp; _.Date &lt;= to.Date)
              .GroupBy(_ =&gt; _.SomeProp)
              .Select(n =&gt; new { n.Key, Count = n.Count() })
              .ToListAsync();
  </code></pre>
  
  <p>works like charm. But lets say that I want to aggregate by custom field - received in aggregateBy string parameter.
  So I try:</p>
  
  <pre><code>var propertyToGroupBy = typeof(DiagnosticEvent).GetProperties()
                          .First(x =&gt; x.Name.ToLowerInvariant() == aggregateBy);
  </code></pre>
  
  <p>And then:</p>
  
  <pre><code>var result = await Events.AsQueryable()
              .Where(_ =&gt; _.Date &gt;= from.Date &amp;&amp; _.Date &lt;= to.Date)
              .GroupBy(p =&gt; propertyToGroupBy.GetValue(p))
              .Select(n =&gt; new { n.Key, Count = n.Count() })
              .ToListAsync();
  </code></pre>
  
  <p>Which ends up in:</p>
  
  <blockquote>
    <p>GetValue of type System.Reflection.PropertyInfo is not supported in
    the expression tree System.String Type.GetValue({document})</p>
  </blockquote>
  
  <p>Any idea how to do it while sticking to LINQ?
  I know that i can use Fluent API with:</p>
  
  <pre><code>var group = new BsonDocument { { "_id", $"${aggregateBy}"}, { "Count", new BsonDocument("$sum", 1) } };
  </code></pre>
  
  <p>But I want to be consistent with LINQ approach.</p>
  
  <p>Full stack trace: <a href="https://pastebin.com/RNzXi1kj" rel="nofollow noreferrer">https://pastebin.com/RNzXi1kj</a></p>