LAMDA表达式学习

1. 普通绑定:

public void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("ok");
        }

this.button1.Click += button1_Click;

2. 匿名委托:

this.button1.Click += delegate(object sender, EventArgs e)
            {
                MessageBox.Show("Click");
            };

 

3. LAMDA表达式:

this.button1.Click += ((sender, e) =>
            {
                MessageBox.Show("Click");
            }
                );

编译器会自动推算出表达式中的类型。

 

4. 过滤条件:

List users = new List();

            Funcbool> predicate = (
                (user) =>
                {
                    return user.UserId > 100;
                }
                );
            List temps = users.Where(predicate).ToList();

等同于:

List temps = users.Where(p => p.UserId > 100).ToList();
List temps = (from p in users where p.UserId > 100 select p).ToList();
List users = new List();
            Funcbool> predicate = (
                (user) => user.UserId > 100
                );
            List temps = users.Where(predicate).ToList();

单挑语句时不需要使用{},同时可以不使用return来返回结果。

5. 排序:

List users = new List();
            List temp1 = users.OrderBy(p=>p.UserId).ToList();
            Funcint> orderby = (user => user.UserId);
            List temp2 = users.OrderBy(orderby).ToList();

 

 

 

 

你可能感兴趣的:(LAMDA表达式学习)