28 理解延迟求值和主动求值之间的区别

在延迟求值的情况下,只是定义一个查询,而不是立刻执行:

    class Program
    {
        static void Main(string[] args)
        {
            List<int> list = new List<int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            var temp1 = from c in list where c > 5 select c;
            var temp2 = (from c in list where c > 5 select c).ToList<int>();
            list[0] = 11;
            foreach (var item in temp1)
            {
                Console.Write(item.ToString() + " ");
            }
            Console.WriteLine();
            foreach (var item in temp2)
            {
                Console.Write(item.ToString() + " ");
            }
        }
    }

输出:

temp1: 11 6 7 8 9

temp2: 6 7 8 9

 

在使用LINQ to SQL时,延迟求值能带来性能提升。

posted @ 2016-01-19 17:13  小-黑  阅读(114)  评论(0)    收藏  举报