C# 带有DynamicExpression.ParseLambda的谓词

C# 带有DynamicExpression.ParseLambda的谓词,c#,C#,我正在构建一个动态过滤列表。我将从UI收到一个谓词(如“Name==”Australia“”),代码将过滤列表。对于“==”操作符来说,它工作得很好。 但是,我正在努力解决以下条件,不确定UI将如何在谓词中传递以下条件 包含 开始于 以 不等于 大于 少于 中间 我在下面复制了我的源代码。在此帮忙将不胜感激 class Program { static void Main(string[] args) { var lst = new

我正在构建一个动态过滤列表。我将从UI收到一个谓词(如“Name==”Australia“”),代码将过滤列表。对于“==”操作符来说,它工作得很好。
但是,我正在努力解决以下条件,不确定UI将如何在谓词中传递以下条件

  • 包含
  • 开始于
  • 不等于
  • 大于
  • 少于
  • 中间
  • 我在下面复制了我的源代码。在此帮忙将不胜感激

    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; }
        }
    
    类程序
    {
    静态void Main(字符串[]参数)
    {
    var lst=新列表();
    lst.Add(newmyclass{Name=“Australia”,Id=1,Dt=System.DateTime.Now.AddDays(10)});
    lst.Add(newmyclass{Name=“USA”,Id=2,Dt=System.DateTime.Now.AddDays(5)});
    添加(新的Myclass{Name=“India”,Id=3,Dt=System.DateTime.Now});
    var result=lst.AsQueryable();
    var r=result.ToList();
    }
    }
    公共类Myclass
    {
    公共int Id{get;set;}
    公共字符串名称{get;set;}
    公共日期时间Dt{get;set;}
    }
    

    我想我找到了我需要的谓词

    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\"))";
    
    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\"))";
    
    文本字段等于=string exp=“(Name=\'India\””;
    文本字段不等于=string exp=“(Name!=“India\”);
    文本字段Contains=string exp=“(Name.Contains(\“Ind\”)”);
    文本字段以=string exp=“(Name.StartsWith(\“Ind\”)开头);
    文本字段以=string exp=“(Name.EndsWith(\'a\”)结尾;
    数字字段等于=字符串exp=“(Id=1)”;
    具有多个条件的数字字段=字符串exp=“(Id>1且Id 1且Id>=3)”;
    日期字段介于=string exp=“(Dt>Convert.ToDateTime(\'2/5/2021 4:46:04 AM\”)和Dt>=Convert.ToDateTime(\'2/3/2021 4:46:04 AM\”)之间);
    
    我想我找到了我需要的谓词

    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\"))";
    
    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\"))";
    
    文本字段等于=string exp=“(Name=\'India\””;
    文本字段不等于=string exp=“(Name!=“India\”);
    文本字段Contains=string exp=“(Name.Contains(\“Ind\”)”);
    文本字段以=string exp=“(Name.StartsWith(\“Ind\”)开头);
    文本字段以=string exp=“(Name.EndsWith(\'a\”)结尾;
    数字字段等于=字符串exp=“(Id=1)”;
    具有多个条件的数字字段=字符串exp=“(Id>1且Id 1且Id>=3)”;
    日期字段介于=string exp=“(Dt>Convert.ToDateTime(\'2/5/2021 4:46:04 AM\”)和Dt>=Convert.ToDateTime(\'2/3/2021 4:46:04 AM\”)之间);
    
    “然而我在以下条件下挣扎”-您尝试了什么,发生了什么?我尝试了谓词“Name Like\%Australia%\”,但它引发了异常。我不确定如何为Like编写谓词。它引发了什么异常?异常类型和消息是什么?当我找到问题的解决方案时,可以结束此问题。我已经用答案更新了我的问题。@Machindra请随意将其作为下面的实际答案发布-回答您自己的问题也意味着您可以接受答案(您得到的回答+该问题不再列为“未回答”)