Skip to content

Instantly share code, notes, and snippets.

Created February 10, 2015 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/95e8f6c0583b0cf6821e to your computer and use it in GitHub Desktop.
Save anonymous/95e8f6c0583b0cf6821e to your computer and use it in GitHub Desktop.
Perform simple "Where" clause on dynamic field of dynamic table
using (var context = new DBEntities())
{
var type = context.GetType();
var tableProperty = type.GetProperty("tableName");
var tableGet = tableProperty.GetMethod;
var tableContent = tableGet.Invoke(context, null);
var tableQuery = (IQueryable)tableContent;
var tableType = tableQuery.ElementType;
var pe = Expression.Parameter(tableType, "tableType");
var left = Expression.PropertyOrField(pe, "fieldName");
var right = Expression.Constant("fieldValue");
var predicateBody = Expression.Equal(left, right);
var whereCallExpression = Expression.Call(typeof(Queryable), "Where", new[] { tableType },
tableQuery.Expression, Expression.Lambda(predicateBody, pe));
IQueryable results = tableQuery.Provider.CreateQuery(whereCallExpression);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment