할 것
링크를 사용합니다. 링크에서 자주 쓰이는 질의인 from, where, orderby, select 절을 사용합니다. select 절에선 무명형식을 사용하여 새로운 형식을 즉석으로 만듭니다.
링크를 사용하여 이름과 키가 적힌 데이터에서 몇 센치 이하의 인물을 추려내는 예제코드를 작성합니다.
코드
using System;
using System.Collections.Generic;
using System.Linq;
namespace LinqSimpleExample
{
class Profile
{
public string Name { get; set; }
public int Height { get; set; }
}
class Program
{
static void Main(string[] args)
{
Profile[] arrProfile =
{
new Profile(){Name = "정우성", Height = 186},
new Profile(){Name = "김태희", Height = 158},
new Profile(){Name = "고현정", Height = 172},
new Profile(){Name = "이문세", Height = 178},
new Profile(){Name = "하하", Height = 171},
};
var profiles = from n in arrProfile
where n.Height < 175
orderby n.Height
select new
{
Name = n.Name,
InchHeight = n.Height * 0.393
};
foreach (var profile in profiles)
Console.WriteLine($"{profile.Name}, {profile.InchHeight}");
}
}
}
출력
5명의 데이터에서 키 175미만의 사람을 출력해냅니다. cm로 표시된 것을 inch로 바꾸어 출력합니다.