This project has moved and is read-only. For the latest updates, please go here.

DateTime Comparison

Jun 9, 2014 at 6:58 PM
I tried to do the following comparison, but get the error 'Cannot resolve symbol "DateTime" at line 1 char 0'. How can i make this comparison?

string expression = "DateTime.Parse('01/06/2014') < DateTime.Parse('09/06/2014')";

CompiledExpression comp = new CompiledExpression(expression);
var eval = comp.Eval();
Jun 10, 2014 at 9:02 AM
Expression Eval initially knows no other symbols other than the basic numeric types (int, long, double).

To get it to recognize other types, use the TypeRegistry class. The RegisterDefaultTypes method of this class will currently register only 3 types: DateTime, Convert and Math.
            string expression = "DateTime.Parse('01/06/2014') < DateTime.Parse('09/06/2014')";

            CompiledExpression comp = new CompiledExpression(expression) { TypeRegistry = new TypeRegistry() };
            comp.TypeRegistry.RegisterDefaultTypes();

            var eval = comp.Eval(); 
Or:
            string expression = "DateTime.Parse('01/06/2014') < DateTime.Parse('09/06/2014')";

            TypeRegistry typeRegistry = new TypeRegistry();
            typeRegistry.RegisterDefaultTypes();

            CompiledExpression comp = new CompiledExpression(expression) { TypeRegistry = typeRegistry };

            var eval = comp.Eval(); 
Or registering specifically:
            string expression = "DateTime.Parse('01/06/2014') < DateTime.Parse('09/06/2014')";

            TypeRegistry typeRegistry = new TypeRegistry();
            typeRegistry.RegisterType("DateTime", typeof(DateTime));

            CompiledExpression comp = new CompiledExpression(expression) { TypeRegistry = typeRegistry };

            var eval = comp.Eval();