Lambda表达式select()和where()的区别
新建一个控制台程序ConsoleApplication1
1、where()用法:必须加条件,且返回对象结果。
static void Main(string[] args)
{
string[] arrays={"asd","abc","bbb","ccc"};
var results = arrays.Where(a=>a.Contains("b"));//必须加条件,返回对象
foreach(var da in results )
{
Console.WriteLine(da);
}
Console.WriteLine("按任意键可退出!");
Console.ReadKey();
}
结果:abc bbb。
2、select()用法:(1)(a=>a.Value=="22")加条件查询时,返回bool型结果;(2)(a=>a)没条件返回对象
(1)(a=>a.Value=="22")加条件查询时,返回bool型结果
static void Main(string[] args)
{
string[] arrays={"asd","abc","bbb","ccc"};
var results = arrays.Select(a => a.Contains("b"));//1、(a=>a.Value=="22")加条件查询时,返回bool型结果
foreach(var da in results )
{
Console.WriteLine(da);
}
Console.WriteLine("按任意键可退出!");
Console.ReadKey();
}
结果:False True True False
(2)(a=>a)没条件返回对象
static void Main(string[] args)
{
string[] arrays={"asd","abc","bbb","ccc"};
var results = arrays.Select(a => a);//1、(a=>a)没条件,返回所有对象;
foreach(var da in results )
{
Console.WriteLine(da);
}
Console.WriteLine("按任意键可退出!");
Console.ReadKey();
}
结果:asd abc bbb ccc