1.딕셔너리로 변환한다.
LINQ에 있는 ToDictionary 메서드를 사용하면 배열이나 리스트를 딕셔너리로 변환할 수 있습니다.
딕셔너리로 변환하면 키를 지정해서 해당 값에 빠르게 접근할 수 있습니다.
다음 코드는 List<Employee>를 Dictionary<int, Employee>로 변환하는 예입니다.
var employees = new List<Employee>();
var employeeDict = employees.ToDictionary(emp => emp.Code);
이 코드에서 ToDictionary 메서드의 첫 번째 인수에 사원 코드(emp.Code)를 나타내는 람다식을 넘겨줍니다. 이렇게 함ㄴ 사원코드를 'KEY'로 이용하고 Employee 객체를 '값'으로 이용해서 딕셔너리를 생성할 수 있습니다.
2. 딕셔너리로 부터 다른 딕셔너리를 생성
var flowerDict = new Dictionary<string, int>() {
["sunflower"] = 400,
["pansy"] = 300,
["tulip"] = 200,
["rose"] = 500,
["dahila"] = 400,
};
var newDict = flowerDict.Where(x => x.Value >= 400)
.ToDictionary(flower => flower.Key, flower => flower.Value);
foreach (var item in newDict.Keys)
{
Debug.Log("value:" + item);
}
댓글 없음:
댓글 쓰기