I would like to run a Json Object
through DynamicExpression
but i am getting the error "No method 'SelectToken' exists on type 'System.Linq.Enumerable'."
the below rule i am trying to execute through DynamicExpression.
Rule: JObject.SelectToken(\"$.Dosage\").ToString()==\"25\"
my Json Object:
{
"Dosage": 25,
"Drug": "Indocin",
"Patient": "David",
"Date": "2019-05-22T22:06:50.4394817"
}
´Code:
//converted json to Json Object:
JArray jsonArray = JArray.Parse(jsonString);
JObject data = JObject.Parse(jsonArray[0].ToString());
bool r = CompareRules("JObject.SelectToken(\"$.Dosage\").ToString()==\"25\"", data);
public static bool CompareRules(string formula, params object[] objects)
{
var result = false;
try
{
var lstParams = objects.Select(o => Expression.Parameter(o.GetType(), o.GetType().Name)).ToArray<ParameterExpression>();
var e = System.Linq.Dynamic.DynamicExpression.ParseLambda(lstParams, null, formula);
result = (bool)e.Compile().DynamicInvoke(objects.ToArray());
}
catch (TargetInvocationException) { }
catch (KeyNotFoundException) { }
catch (Exception ex) { }
return result;
}
i expect the output of JObject.SelectToken(\"$.Dosage\").ToString()==\"25\"
is true
if the value matches otherwise false.