|
Private void button2_Click(object sender, EventArgs e)
{
List<Customer> myCustomer = GetCustomerList();
//c는 익명 타입
//myCustomer의 값 중 하나가 c에 대입하여 이를 사용
IEnumerable<Customer> ie = myCustomer.Where(c => c.City == "서울");
//위를 다음과 같이 익명 함수를 사용하여 작성할 수 있음
//IEnumerable<Customer> ie = myCustomer.Where(
// delegate(Customer c)
// {
// return (c.City == "서울" ? true : false);
// }
//);
foreach (Customer cs in ie)
{
MessageBox.Show(cs.Name);
}
}
private void button3_Click(object sender, EventArgs e)
{
List<Customer> myCustomer = GetCustomerList();
double avg = myCustomer.Average(c => c.Age);
//위와 동일한 코드
//double avg = myCustomer.Average(
// delegate(Customer c)
// {
// return c.Age;
// }
//);
MessageBox.Show(avg.ToString());
}
private List<Customer> GetCustomerList()
{
List<Customer> myCustomer = new List<Customer>();
myCustomer.Add(new Customer() { Name = "홍길동", Age = 10, City = "서울" });
myCustomer.Add(new Customer() { Name = "김길덩", Age = 20, City = "부산" });
myCustomer.Add(new Customer() { Name = "안길둥", Age = 30, City = "대구" });
myCustomer.Add(new Customer() { Name = "원갈동", Age = 40, City = "광주" });
myCustomer.Add(new Customer() { Name = "문걸동", Age = 50, City = "서울" });
myCustomer.Add(new Customer() { Name = "조골동", Age = 60, City = "서울" });
return myCustomer;
} |