[开发故事]第二回:LINQ来了,先忘记foreach2011-05-19 博客园 Anytao你看,LINQ已经全面光临了,本文并非探讨LINQ的是是非非,而是通过自己开 发过程的一个小小的侧面来展示,LINQ已经来了,而且更美好。对技术而言,创新的最大敌人,是转换固有思维,而不是技术本身。1 引言今天,Terry点敲了我对于一段代码的处理,诚如本文标题所言,事件缘起于 我对一个List<T>转换的小小处理。首先来看看,这个List<T>转换 双方的本来面目,以一个常见的User类为例而言,User类代表了Model层的实体类 ,其定义为:// Release : code10, 2008/10/06 // Author : Anytao, http://www.anytao.com public class User { public int ID { get; set; } public string FirstName { get; set; } public string SecondName { get; set; } public int Age { get; set; } }而Account类,则代码了Business Object层的业务类,其定义为:// Release : code10, 2008/10/06 // Author : Anytao, http://www.anytao.com public class Account { public int ID { get; set; } public string Name { get; set; } public int Age { get; set; } }那么一件重要的事情,就是如何完成二者之间的转换,尤其是,类似于 List<User>到List<Account>这样的转换,是常常发生在业务处理的 实际操作中。关于二者的区别,属于设计方面的论题,不是本文关注的对象。2 本来的实现---想起来就是foreach好了,典型的List<T>转换,我们固有思维中想到的就是循环了,所以 我想都没想就实现了下面的处理过程:// Release : code10, 2008/10/06 // Author : Anytao, http://www.anytao.com public List<Account> GetAccounts(List<User> users) { List<Account> accounts = new List<Account> ();