13
2024
08
19:10:28

[Lambda表达式]简单应用

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




推荐本站淘宝优惠价购买喜欢的宝贝:

image.png

本文链接:https://www.hqyman.cn/post/7544.html 非本站原创文章欢迎转载,原创文章需保留本站地址!

分享到:
打赏





休息一下~~


« 上一篇 下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

请先 登录 再评论,若不是会员请先 注册

您的IP地址是: