Created
February 10, 2015 16:55
-
-
Save anonymous/95e8f6c0583b0cf6821e to your computer and use it in GitHub Desktop.
Perform simple "Where" clause on dynamic field of dynamic table
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 (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