I am constructing a dynamic filtering on list. I will receive a predicate from UI (like "Name == "Australia"") and the code should filter the list. It's working fine for "==" operator.
However I am struggling with below conditions and not sure how UI will pass the below conditions in predicate.
- Contains
- Starts With
- End With
- Not Equal To
- Greater Than
- Less Than
- Between
I have copied my source code below. Help here would be appreciated.
class Program
{
static void Main(string[] args)
{
var lst = new List<Myclass>();
lst.Add(new Myclass { Name = "Australia", Id = 1, Dt = System.DateTime.Now.AddDays(10) });
lst.Add(new Myclass { Name = "USA", Id = 2, Dt = System.DateTime.Now.AddDays(5) });
lst.Add(new Myclass { Name = "India", Id = 3, Dt = System.DateTime.Now});
var result = lst.AsQueryable().Where(DynamicExpression.ParseLambda<Myclass, bool>("Name == "Australia""));
var r = result.ToList();
}
}
public class Myclass
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime Dt { get; set; }
}
I think I found the predicate which I need.
Text Field Equal To = string exp = "(Name ="India")";
Text Field NOT Equal To = string exp = "(Name !="India")";
Text Field Contains = string exp = "(Name.Contains ("Ind"))";
Text Field Starts With = string exp = "(Name.StartsWith ("Ind"))";
Text Field Ends With = string exp = "(Name.EndsWith ("a"))";
Number Field Equal to = string exp = "(Id = 1)";
Number Field Between with multiple conditions = string exp = "(Id > 1 AND Id <= 3) OR Id =3";
Number Field Between and greater than equal to = string exp = "(Id > 1 AND Id >= 3)";
Date Field Between = string exp = "(Dt > Convert.ToDateTime("2/5/2021 4:46:04 AM") AND Dt >= Convert.ToDateTime("2/3/2021 4:46:04 AM"))";
question from:
https://stackoverflow.com/questions/66062069/predicate-with-dynamicexpression-parselambda 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…