title: Using LINQ to evaluate expressions from a string against a datatable
body: <p>I am trying to write something that will allow custom expressions be stored in strings to be evaluated against various data.  After several failed attempts I was able to find something successful, but it doesn't lend itself to the intent.</p>
  
  <pre class="lang-cs prettyprint-override"><code>DataTable dt = new DataTable();
  dt.Columns.Add(new DataColumn("Name", typeof(string)));
  dt.Columns.Add(new DataColumn("Age", typeof(int)));
  
  dt.Rows.Add(new object[] { "John", 32 });
  
  var test = dt.AsEnumerable().AsQueryable();
  
  // An example of an expression to test
  string expression = "Age = 32";
  
  // Failed Attempts
  //var result1 = test.Where(expression); // Error received:  {"No property or field 'Age' exists in type 'DataRow'"}
  //var result2 = test.Where("it.Field&lt;object&gt;(\"Age\") == 32");  // Error received:  {"No property or field 'Field' exists in type 'DataRow'"}
  //var result3 = test.Where("Age=@0", 32);  // Error received:  {"No property or field 'Age' exists in type 'DataRow'"}
  //var result4 = test.Where("it[\"Age\"] == 127"); // Error received:  {"Operator '==' incompatible with operand types 'Object' and 'Int32'"}
  //var result5 = test.Where("it[\"Age\"] = 127");  // Error received:  {"Operator '=' incompatible with operand types 'Object' and 'Int32'"}
  //var result6 = test.Where("it[\"Age\"] as Age  = 127");  // Error received:  {"Expression of type 'Boolean' expected"}
  
  // Successful but at this point its not really a dynamic linq expression
  var result = test.Where(it =&gt; it.Field&lt;int&gt;("Age") == 32);
  </code></pre>
  
  <p>How can I accomplish this?  All roads seems to lead back to this article:  <a href="http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx" rel="nofollow">http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx</a>  But I do not see the answer to what I am trying to do as he is using strongly typed data.</p>
  
  <p>Thanks! </p>