與SQL命令中的Where作用相似芬首,起到過濾作用畴博。
一個(gè)員工安排到一輛車上瓢捉,一輛車可以有很多員工频丘,車牌號(hào)和員工號(hào)是一對(duì)多的關(guān)系。
員工類
public class Employee
{
/// <summary>
/// 車牌號(hào)
/// </summary>
public int CarNo { get; set; }
/// <summary>
/// 員工號(hào)
/// </summary>
public string EmployeeNo { get; set; }
/// <summary>
/// 描述
/// </summary>
public string Description { get; set; }
/// <summary>
/// 年齡
/// </summary>
public int Age { get; set; }
}
獲取員工數(shù)據(jù)方法
public static List<Employee> GetList()
{
return new List<Employee>()
{
new Employee(){ CarNo=1,EmployeeNo="NO-1",Description ="廣州分公司",Age=21},
new Employee(){ CarNo=2,EmployeeNo="NO-2",Description ="惠州分公司",Age=30},
new Employee(){ CarNo=3,EmployeeNo="NO-3",Description ="東莞分公司",Age=27},
new Employee(){ CarNo=4,EmployeeNo="NO-4",Description ="廣州分公司",Age=28},
new Employee(){ CarNo=3,EmployeeNo="NO-4",Description ="東莞分公司",Age=52},
new Employee(){ CarNo=1,EmployeeNo="NO-5",Description ="深圳分公司",Age=18},
new Employee(){ CarNo=1,EmployeeNo="NO-6",Description ="中山分公司",Age=21},
new Employee(){ CarNo=2,EmployeeNo="NO-7",Description ="廣州分公司",Age=31},
new Employee(){ CarNo=5,EmployeeNo="NO-8",Description ="佛山分公司",Age=19},
new Employee(){ CarNo=6,EmployeeNo="NO-9",Description ="深圳分公司",Age=20}
};
}
獲取年齡大于30歲的所有員工信息
var employeeList = Employee.GetList();
var list = employeeList.Where(x => x.Age > 30);
StringBuilder sb = new StringBuilder();
foreach (var item in list)
{
sb.AppendFormat("員工號(hào){0}泡态,描述:{1}搂漠,年齡:{2}<br/> ", item.EmployeeNo, item.Description, item.Age);
}
輸出結(jié)果:
員工號(hào)NO-4,描述:東莞分公司某弦,年齡:52
員工號(hào)NO-7桐汤,描述:廣州分公司而克,年齡:31
獲取年齡大于20歲并且坐1號(hào)車的所有員工信息
var employeeList = Employee.GetList();
var list = employeeList.Where(x => x.Age > 20 && x.CarNo == 1);
StringBuilder sb = new StringBuilder();
foreach (var item in list)
{
sb.AppendFormat("員工號(hào){0},描述:{1}怔毛,年齡:{2}<br/> ", item.EmployeeNo, item.Description, item.Age);
}
或者
var employeeList = Employee.GetList();
var list = employeeList.Where(x => x.Age > 20).Where(x => x.CarNo == 1);
StringBuilder sb = new StringBuilder();
foreach (var item in list)
{
sb.AppendFormat("員工號(hào){0}员萍,描述:{1},年齡:{2}<br/> ", item.EmployeeNo, item.Description, item.Age);
}
輸出結(jié)果:
員工號(hào)NO-1拣度,描述:廣州分公司碎绎,年齡:21
員工號(hào)NO-6,描述:中山分公司蜡娶,年齡:21