I am trying to loop through all properties in an object including nested objects and objects in collections to generate LINQ expressions. The structure of my classes as follows:
public class Rule
{
public string label { get; set; }
public string field { get; set; }
public string @operator { get; set; }
public string type { get; set; }
public string value { get; set; }
public string condition { get; set; }
public List<Rule> rules { get; set; }
}
public class QueryBuilder
{
public string condition { get; set; }
public List<Rule> rules { get; set; }
}
bellow is the example JSON for the above entity mapping
{
"condition": "and",
"rules": [
{
"condition": "or",
"rules": [
{
"label": "PaymentMode",
"field": "PaymentMode",
"operator": "equal",
"type": "string",
"value": "Cash"
},
{
"label": "TransactionType",
"field": "TransactionType",
"operator": "equal",
"type": "string",
"value": "expensive"
}
]
},
{
"condition": "or",
"rules": [
{
"label": "Date",
"field": "Date",
"operator": "equal",
"type": "date",
"value": "4/15/21"
},
{
"label": "Date",
"field": "Date",
"operator": "equal",
"type": "date",
"value": "4/15/21"
}
]
}
]
}
The above json will be in a hierarchy. User can select any number of AND or OR condition. Need to make bellow expression inside a recursive loop
var constant = Expression.Constant("Jhon");
var property = Expression.Property(paramExpr,"FirstName");
expression = Expression.Equal(property, constant);
constant = Expression.Constant("Ram");
property = Expression.Property(paramExpr, "LastName");
var expression2 = Expression.Equal(property, constant);
expression = Expression.Or(expression, expression2);
How can I make a recursive loop with the above structure.