[Lambda表达式]简单应用原创
金蝶云社区-筱泉
筱泉
18人赞赏了该文章 3,436次浏览 未经作者许可,禁止转载编辑于2021年09月03日 11:28:33
 
 lambda表达式多条件查询

并且,与或者:&&与||

clist = clist.Where(r => r.Price > 50 && r.Price < 60 || r.Price < 30).ToList();


包含:contains

 List<car> clist= new cardata().SelectAll().Where(r => r.Name.Contains(TextBox1.Text)).ToList();

 等于:==

 clist = clist.Where(r => r.Price==40).ToList();

  clist = clist.Where(r => r.Price.Equals(40)).ToList();

 以。。开始、以。。结束

 List<users> ulist = con.users.Where(r => r.username.StartsWith("s")).ToList();
            List<users> ulsit = con.users.Where(r => r.username.EndsWith("t")).ToList();

最大值、最小值、平均值、求和

 int s = con.users.Max(r=>r.ids);
            int t = con.users.Min(r=>r.ids);
            double h = con.users.Average(r => r.ids);
            int d = con.users.Sum(r => r.ids);

求交集

            List<users> ulist1 = new List<users>();
            List<users> ulist2 = new List<users>();
            List<users> ulist3 = new List<users>();
            List<users> ulist = ulist1.Intersect(ulist2).Intersect(ulist3).ToList();    

顺序、倒序

            List<users> ulist = con.users.OrderBy(r => r.ids).ToList();
            List<users> ulist1 = con.users.OrderByDescending(r => r.ids).ToList();




  //******* 对集合按Name属于进行分组GroupBy查询 ********  
            //结果中包括的字段:  
            //1、分组的关键字:Name = g.Key  
            //2、每个分组的数量:count = g.Count()  
            //3、每个分组的年龄总和:ageC = g.Sum(item => item.Age)  
            //4、每个分组的收入总和:moneyC = g.Sum(item => item.Money)  
      
            //写法1:Lambda 表达式写法(推荐)  
            var ls = persons1.GroupBy(a => a.Name).Select(g => (new { name = g.Key, count = g.Count(), ageC = g.Sum(item => item.Age), moneyC = g.Sum(item => item.Money) }));  
            //写法2:类SQL语言写法 最终编译器会把它转化为lamda表达式  
            var ls2 = from ps in persons1  
                     group ps by ps.Name  
                         into g  
                         select new { name = g.Key, count = g.Count(), ageC = g.Sum(item => item.Age), moneyC = g.Sum(item => item.Money) };  
      
            dataGridView1.DataSource = ls.ToList();  
           //dataGridView1.DataSource = ls2.ToList();  
        }  

List.Where(new Func<Class, bool>(Where));
 
        private bool Where(Class c)
        {
            //组织表达式,返回bool
        }


Expression<Func<XXX, bool>> expression = t => true;
expression = expression.And(t => t.AAA == 1);
expression = expression.And(t => t.BBB > 1);
var ds = products.AsQueryable().Where(expression).ToList();



public static class PredicateExtensions
    {
        public static Expression<Func<T, bool>> True<T>()
        {
            return f => true;
        }
 
        public static Expression<Func<T, bool>> False<T>()
        {
            return f => false;
        }
 
        public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expression1, Expression<Func<T, bool>> expression2)
        {
            var invokedExpression = Expression.Invoke(expression2, expression1.Parameters.Cast<Expression>());
            return Expression.Lambda<Func<T, bool>>(Expression.Or(expression1.Body, invokedExpression), expression1.Parameters);
        }
 
        public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expression1, Expression<Func<T, bool>> expression2)
        {
            var invokedExpression = Expression.Invoke(expression2, expression1.Parameters.Cast<Expression>());
            return Expression.Lambda<Func<T, bool>>(Expression.And(expression1.Body, invokedExpression), expression1.Parameters);
        }
    }




图标赞 18
18人点赞
还没有人点赞,快来当第一个点赞的人吧!
图标打赏
0人打赏
还没有人打赏,快来当第一个打赏的人吧!