Skip to content

Instantly share code, notes, and snippets.

@BenjaminAdams
Last active August 14, 2019 00:12
Show Gist options
  • Save BenjaminAdams/2f3aa5dc4fc4477c3887 to your computer and use it in GitHub Desktop.
Save BenjaminAdams/2f3aa5dc4fc4477c3887 to your computer and use it in GitHub Desktop.
c# doing dynamic GroupBy clause with Linq to SQL
using System.Linq;
using System.Linq.Dynamic;
using System.Linq.Expressions;
ParameterExpression p = Expression.Parameter(typeof(SomeEfClass), "p");
var selector = Expression.Lambda < Func < SomeEfClass,
string >> (Expression.Property(p, input.requestedField), p);
retVal = datacontext.AsQueryable()
.GroupBy(selector)
.Select(q => new SomeReturnTypeClass {
Text = q.Key,
Value = q.Count().ToString()
}).ToList();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment