Lambda表达式格式:(参数列表)=>表达式或语句块.
具体意义:定义Lambda接受参数列表,运行表达式或语句块返回表达式或语句块的值传给这个参数列表。
Lambda表达式参数类型可以是隐式类型或显式类型。在显式列表中,每个参数的类型是显式指定的,在隐式列表中,参数的类型由Lambda表达式出现的语境自动推断类型。Lambda表达式的参数列表可以有一个或多个参数,或者无参数。在有单一的隐型参数的lambda表达式中,圆括号可以从参数列表中省略。
先看DEMO:
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class LambdaExpressions : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
List<Staff> liStaff = new List<Staff>{
new Staff{id=1,name="syf",HomeTown="山西"},
new Staff{id=2,name="zjy",HomeTown="云南"},
new Staff {id= 3,name="qj",HomeTown = "江西" }};
var result = liStaff.Where(s => s.HomeTown == "江西").ToList();
Response.Write(result[0].name);
}
public class Staff
{
public int id { get; set; }
public string name { get; set; }
public string HomeTown { get; set; }
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class LambdaExpressions : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
List<Staff> liStaff = new List<Staff>{
new Staff{id=1,name="syf",HomeTown="山西"},
new Staff{id=2,name="zjy",HomeTown="云南"},
new Staff {id= 3,name="qj",HomeTown = "江西" }};
var result = liStaff.Where(s => s.HomeTown == "江西").ToList();
Response.Write(result[0].name);
}
public class Staff
{
public int id { get; set; }
public string name { get; set; }
public string HomeTown { get; set; }
}
}
}