Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
159 views
in Technique[技术] by (71.8m points)

c# - Predicate with DynamicExpression.ParseLambda

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.

  1. Contains
  2. Starts With
  3. End With
  4. Not Equal To
  5. Greater Than
  6. Less Than
  7. 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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"))";

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...