Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to convert a String to its equivalent LINQ Expression Tree?
    primarykey
    data
    text
    <p>This is a simplified version of the original problem.</p> <p>I have a class called Person:</p> <pre><code>public class Person { public string Name { get; set; } public int Age { get; set; } public int Weight { get; set; } public DateTime FavouriteDay { get; set; } } </code></pre> <p>...and lets say an instance:</p> <pre><code>var bob = new Person { Name = "Bob", Age = 30, Weight = 213, FavouriteDay = '1/1/2000' } </code></pre> <p>I would like to write the following as a <em>string</em> in my favourite text editor....</p> <pre><code>(Person.Age &gt; 3 AND Person.Weight &gt; 50) OR Person.Age &lt; 3 </code></pre> <p>I would like to take this string and my object instance and evaluate a TRUE or FALSE - i.e. evaluating a Func&lt;Person, bool> on the object instance.</p> <p>Here are my current thoughts:</p> <ol> <li>Implement a basic grammar in ANTLR to support basic Comparison and Logical Operators. I am thinking of copying the Visual Basic precedence and some of the featureset here: <a href="http://msdn.microsoft.com/en-us/library/fw84t893(VS.80).aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/fw84t893(VS.80).aspx</a></li> <li>Have ANTLR create a suitable AST from a provided string.</li> <li>Walk the AST and use the <a href="http://www.albahari.com/nutshell/predicatebuilder.aspx" rel="noreferrer">Predicate Builder</a> framework to dynamically create the Func&lt;Person, bool></li> <li>Evaluate the predicate against an instance of Person as required</li> </ol> <p><strong>My question is have I totally overbaked this? any alternatives?</strong></p> <hr> <h2>EDIT: Chosen Solution</h2> <p>I decided to use the Dynamic Linq Library, specifically the Dynamic Query class provided in the LINQSamples.</p> <p>Code below:</p> <pre><code>using System; using System.Linq.Expressions; using System.Linq.Dynamic; namespace ExpressionParser { class Program { public class Person { public string Name { get; set; } public int Age { get; set; } public int Weight { get; set; } public DateTime FavouriteDay { get; set; } } static void Main() { const string exp = @"(Person.Age &gt; 3 AND Person.Weight &gt; 50) OR Person.Age &lt; 3"; var p = Expression.Parameter(typeof(Person), "Person"); var e = System.Linq.Dynamic.DynamicExpression.ParseLambda(new[] { p }, null, exp); var bob = new Person { Name = "Bob", Age = 30, Weight = 213, FavouriteDay = new DateTime(2000,1,1) }; var result = e.Compile().DynamicInvoke(bob); Console.WriteLine(result); Console.ReadKey(); } } } </code></pre> <p>Result is of type System.Boolean, and in this instance is TRUE.</p> <p>Many thanks to Marc Gravell.</p> <p>Include <a href="https://www.nuget.org/packages/System.Linq.Dynamic/" rel="noreferrer">System.Linq.Dynamic</a> nuget package, documentation <a href="https://github.com/kahanu/System.Linq.Dynamic/wiki/Dynamic-Expressions" rel="noreferrer">here</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload