title: Dynamic ObjectQuery<T> against EF 4 using parsed Entity SQL
body: <p>I am trying to create a completely dynamic way to query entity framework using entity sql, where the type T in ObjectQuery (the table name, or entity name) is not known at compile time, and is passed into a method as a string,</p>
  
  <pre><code>    public class EntityQuery
      {
           public ObjectContext Context { get; private set; }
           public string TableName { get; private set; }
  
           public EntityQuery(ObjectContext context, string sourceObject)
           {
                MemberInfo[] mInfo = context.GetType().GetMembers();
  
                TableName = sourceObject; 
                Context = context;
  
                if (!mInfo.Any&lt;MemberInfo&gt;(MemberInfo =&gt; MemberInfo.Name == TableName))
                     throw new EntityOperationException("Entity '" + TableName + "' does not exist in the object context.");
           }
  
           public ObjectQuery&lt;dynamic&gt; InitiateQuery(List&lt;EntityFilter&gt; filters)
           {
                string predicate = DynamicESQLBuilder.GetESQL(filters, Context.DefaultContainerName, TableName);
                string format = String.Format("[{0}]", predicate);
  
                ObjectQuery&lt;dynamic&gt; query = new ObjectQuery&lt;dynamic&gt;(predicate, Context, MergeOption.NoTracking);
  
                return query;
           }
  }
  </code></pre>
  
  <p>The class DynamicESQLBuilder creates an entity sql command string based on the context name, table name, and entity filters passed in. The 'EntityFilter' class essentially encapsulates a single 'where' clause,</p>
  
  <pre><code>  public class EntityFilter
    {
        public ExpressionType OpType { get; private set; }
        public string PropertyName { get; private set; }
        public object Value { get; private set; }
    }
  </code></pre>
  
  <p>When I try to use it like this,</p>
  
  <pre><code>        MyDBModel db = new MyDBModel();
  
          List&lt;EntityFilter&gt; filters = new List&lt;EntityFilter&gt;()
          {
              new EntityFilter("Name", ExpressionType.Equal, "Sean")
          };
  
          EntityQuery query = new EntityQuery(db, "Person");
  
          var results = query.InitiateQuery(filters);
  </code></pre>
  
  <p>I get an error stating:</p>
  
  <p>'Name' is not a member of type 'MyDBModel.Person' in the currently loaded schemas. Near simple identifier, line 1, column 74.</p>
  
  <p>I am open to an entirely different way of doing this. But basically what I am trying to achieve is similar to what WCF OData services do behind the scenes -- I can query my model with just strings passed in - the name of the entity, the operation, the value, etc. No need for a strongly typed 'T' in my query. </p>