天天看點

“拼接”linq查詢

static void Main(string[] args)

        {

            List<Person> list = new List<Person>() { 

                new Person(){ Age=20, Name="wengyuli", Parts=new Part(){ Name="123"}},

                new Person(){ Age=30, Name="chenyang", Parts=new Part(){ Name="123456"}},

                new Person(){ Age=40, Name="heshuai", Parts=new Part(){ Name="123"}},

                new Person(){ Age=50, Name="heyunlong", Parts=new Part(){ Name="123456"}}

            };

            var c = Expression.Parameter(typeof(Person), "m");

            Expression condition = Expression.Constant(false);

            Expression con = Expression.Call(

                Expression.Property(c,typeof(Person).GetProperty("Name")),

                typeof(string).GetMethod("StartsWith",new Type[]{typeof(string)}),

                Expression.Constant("wengyuli"));

            condition = Expression.Or(con, condition); 

            Expression<Func<Person, bool>> end = Expression.Lambda<Func<Person, bool>>(condition, new ParameterExpression[] { c });

        }

        class Person

        {

            public string Name { set; get; }

            public int Age { set; get; }

            public Part Parts { set; get; }

        class Part

    }

     本文轉自wengyuli 51CTO部落格,原文連結:http://blog.51cto.com/wengyuli/586676,如需轉載請自行聯系原作者

繼續閱讀