Last active
August 14, 2019 00:12
-
-
Save BenjaminAdams/2f3aa5dc4fc4477c3887 to your computer and use it in GitHub Desktop.
c# doing dynamic GroupBy clause with Linq to SQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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