首先看一下Lambda长什么样子:Func<int,string> = num=> return num.ToString(); 它由三部分组成等于号“=”左边为第一部分为函数,就是要使用Lambda的函数,并且函数中可以有参数,也可以不带参数,然后还有一个返回值;等于号“=”右边,"=>"左边为第二部分为抽象参数,用于传值;第三部分为"=>"右边部分为第三部分,方法的具体实现,以及返回的内容;那么什么时候下和什么情况下用得到Lambda表达式呢?
我个人在工作中总结的lambda使用有如下四个地方,极大地简化了操作,如果不同观点请大神不吝指正,我必定诚心接受,在此先谢过大神们了!
一、在查询数组或者List泛型或者DataTable中特定内容时使用,等同于查询功能吧。 举例为证:
有一个List泛型吧,它包含一个Student类,该类中有:姓名、年龄、性别、城市 四个属性,那么问题来了:
1、查询姓名中,名字带有“李”不一定姓“李”,只要名字带有“李”即可,所有学生;
第一步,定义这个Students类
第三步:关键一步,执行Lambda
1、
// 查询姓名中,名字带有“李”不一定姓“李”,只要名字带有“李”即可,所有学生;
//查询年龄在30到40之间的学生
3、
//查询年龄在30到40之间的学生并以年龄由大到小反向排序
查看效果吧:
二、在委托中使用:
其中,我注释的地方为原先委托的普通写法,这样是标准写法,但是自从用了Lambda之后啊,后面的方法就完全可以不写了,是不是很方便呢?
三、 在线程中使用Lambda,这跟用委托使用是一样的,因为 线程也好,事件也好都是一种委托嘛~
四、这是Lambda最典型的使用:Func<>的使用
Func<int,int> num = a => a*a;
其中第一个int为参数类型,第二个int为返回值类型,也可以多个参数:
Func<int,int,int,string> method1= (a,b,c) => (a+b)*c;
也可以写的复杂一点,加上return
使用:method2(12,13,9);
好了,先总结这一些吧,如有其它问题,欢迎纰漏~!
我个人在工作中总结的lambda使用有如下四个地方,极大地简化了操作,如果不同观点请大神不吝指正,我必定诚心接受,在此先谢过大神们了!
一、在查询数组或者List泛型或者DataTable中特定内容时使用,等同于查询功能吧。 举例为证:
有一个List泛型吧,它包含一个Student类,该类中有:姓名、年龄、性别、城市 四个属性,那么问题来了:
1、查询姓名中,名字带有“李”不一定姓“李”,只要名字带有“李”即可,所有学生;
2、查询年龄在30到40之间的学生(假设而已嘛,就是有这么大年龄的学习不止,奋斗不止的大叔去上学嘛,对
吧?)
3、查询2中内容以年龄进行排序;
第一步,定义这个Students类
<span style="font-size:18px;"></span><pre name="code" class="csharp"><span style="font-size:18px;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestLambda01
{
public class Students
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private string city;
public string City
{
get { return city; }
set { city = value; }
}
private int age;
public int Age
{
get { return age; }
set { age = value; }
}
private string sex;
public string Sex
{
get { return sex; }
set { sex = value; }
}
}
} </span>
第二步: 在主函数中生命并初始化一个List<Students>对象,以便用于查找内容;<span style="font-size:18px;"></span><pre name="code" class="csharp"><span style="font-size:18px;">namespace TestLambda01
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
private List<Students> stusList;
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
stusList = new List<Students>()
{
new Students{Name="李继鹏",Sex="男",Age=20,City="临沂"},
new Students{Name="李昌远",Sex="女",Age=22,City="临沂"},
new Students{Name="李杨",Sex="男",Age=30,City="日照"},
new Students{Name="李玉民",Sex="男",Age=33,City="济南"},
new Students{Name="孔鹏举",Sex="男",Age=25,City="济宁"},
new Students{Name="韩国振",Sex="男",Age=20,City="菏泽"},
new Students{Name="张倩",Sex="女",Age=23,City="济宁"},
new Students{Name="万龙",Sex="男",Age=22,City="孝感"},
new Students{Name="李笠存",Sex="男",Age=34,City="商丘"},
new Students{Name="鞠仕磊",Sex="男",Age=24,City="深圳"},
new Students{Name="隔壁老王",Sex="男",Age=44,City="广州"}
};
listBox1.ItemsSource = stusList;
}
}
} </span>
第三步:关键一步,执行Lambda
1、
// 查询姓名中,名字带有“李”不一定姓“李”,只要名字带有“李”即可,所有学生;
<pre name="code" class="csharp"><span style="font-size:18px;">private void button1_Click(object sender, RoutedEventArgs e)
{
listBox2.ItemsSource = null;
IEnumerable<Students> ienumlist = stusList.Where(p => p.Name.IndexOf('李') >= 0);
List<Students> new_studentlist = ienumlist .ToList();
listBox2.ItemsSource = new_studentlist;
} </span>
2、 //查询年龄在30到40之间的学生
<span style="font-size:18px;"></span><pre name="code" class="csharp"><span style="font-size:18px;">private void button1_Click(object sender, RoutedEventArgs e)
{
listBox2.ItemsSource = null;
IEnumerable<Students> ienum_students = stusList.Where(p => p.Age >= 30 && p.Age < 40);
List<Students> new_studentlist = ienum_students.ToList();
listBox2.ItemsSource = new_studentlist;
} </span>
3、
//查询年龄在30到40之间的学生并以年龄由大到小反向排序
<span style="font-size:18px;"></span>
<span style="font-size:18px;">private void button2_Click(object sender, RoutedEventArgs e)
{
listBox2.ItemsSource = null;
List<Students> newlist = stusList.Where(p => p.Age >= 30 && p.Age < 40).OrderByDescending(r => r.Age).ToList();
listBox2.ItemsSource = newlist;
} </span>
查看效果吧:
二、在委托中使用:
<pre name="code" class="csharp"><span style="font-size:18px;">namespace TestLambda01
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public delegate void TestDelegate(string str);
public TestDelegate testDelegate;
public MainWindow()
{
InitializeComponent();
}
private void button3_Click(object sender, RoutedEventArgs e)
{
//testDelegate = new TestDelegate(RealizeDelegate);
//testDelegate("Hello World!");
testDelegate = str =>
{
MessageBox.Show("祝大家新年快乐!身体健康!测试结果为:" + str);
};
testDelegate("钱钱钱钱钱钱!");
}
public void RealizeDelegate(string stra)
{
MessageBox.Show("祝大家新年快乐!身体健康!测试结果为:" + stra);
}
private void button4_Click(object sender, RoutedEventArgs e)
{
Func<int, int, string> funwc = (a, b) =>
{
if (a>10)
{
return "祝大家新年快乐!学业进步,事业有成!测试结果为:" + b.ToString();
}
else
{
return "再次祝大家新年快乐!财源广进,蒸蒸日上!测试结果为:" + b.ToString();
}
};
MessageBox.Show(funwc(12, 100));
}
}
}</span>
其中,我注释的地方为原先委托的普通写法,这样是标准写法,但是自从用了Lambda之后啊,后面的方法就完全可以不写了,是不是很方便呢?
三、 在线程中使用Lambda,这跟用委托使用是一样的,因为 线程也好,事件也好都是一种委托嘛~
<span style="font-size:18px;">public void RealizeDelegate(string stra)
{
MessageBox.Show("祝大家新年快乐!身体健康!测试结果为:" + stra);
//Thread td = new Thread(new ThreadStart(Dothread));
//td.Start();
Thread td = new Thread(new ThreadStart(() =>
{
MessageBox.Show("新年好!");
}));
td.Start();
}
void Dothread()
{
MessageBox.Show("新年好!");
} </span>
Func<int,int> num = a => a*a;
其中第一个int为参数类型,第二个int为返回值类型,也可以多个参数:
Func<int,int,int,string> method1= (a,b,c) => (a+b)*c;
也可以写的复杂一点,加上return
<span style="font-size:18px;">Func<int,int,int,string> method2= (a,b,c) =>
{
if(c>10)
{
return (a+b)*c;
}
else
{
return(a - b + c) * 10
}
}; </span>
使用:method2(12,13,9);
<span style="font-size:18px;">private void button4_Click(object sender, RoutedEventArgs e)
{
Func<int, int, string> funwc = (a, b) =>
{
if (a > 10)
{
return "鹏举祝大家新年快乐!学业进步,事业有成!测试结果为:" + b.ToString();
}
else
{
return "李杨祝大家新年快乐!财源广进,蒸蒸日上!测试结果为:" + b.ToString();
}
};
MessageBox.Show(funwc(12, 100));
} </span>
好了,先总结这一些吧,如有其它问题,欢迎纰漏~!