Linq 透明标识符 let

 

 

 

            IEnumerable<Person> list = new List<Person>
            {
                new Person {Age = 12, Id = 2},
                new Person {Age = 22, Id = 3},
                new Person {Age = 15, Id = 1},
                new Person {Age = 19, Id = 4},
                new Person {Age = 10, Id = 6},
                new Person {Age = 32, Id = 5},
            };

            var result = from p in list
                             //透明标识符 let , 避免了计算两次 p.Id 的平方
                         let length = Math.Pow(p.Id, 2)
                         orderby length
                         select new { p.Age, length };

            foreach (var person in result)
            {
                Console.WriteLine(person.length + ":" + person.Age);
            }

 

posted @ 2018-04-01 01:01  热敷哥  阅读(258)  评论(0编辑  收藏  举报