(.NET) let

C#/Winform 2017. 4. 15. 15:00
728x90
728x170

let : 범위 변수를 기반으로 구성되는 새로운 범위 변수           

 

            People p1 = new People() { name = "A", age = 4 };
            People p2 = new People() { name = "B", age = 5 };
            People p3 = new People() { name = "C", age = 6 };
            People p4 = new People() { name = "E", age = 7 };
            People p5 = new People() { name = "F", age = 8 };

            List<People> pList = new List<People>() { p1, p2, p3, p4, p5 };

 

            var query = from user in pList
                        orderby user.name.Length
                        select user.name;

            foreach (var name in query)
            {
                Console.WriteLine("{0},{1}", name, name.Length);
            }

            // 위처럼 하면 name 의 length 를 두번이나 구해야한다.

 

            var letQuery = from user in pList
                        let length = user.name.Length
                        orderby length
                        select new { Name = user.name, Length = length };

            foreach (var entry in letQuery)
            {
                Console.WriteLine("{0},{1}", entry.Name, entry.Length);
            }

            // 하지만 위처럼 let 를 이용하면 length 는 한번만 구하면 된다.

 

728x90
그리드형
Posted by kjun.kr
,