You see, LINQ has come to the full, this article is not to explore the right and wrong of LINQ, but through their own development process of a small side to show that LINQ has arrived, and better.
For technology, the biggest enemy of innovation is the transformation of the inherent thinking, not the technology itself.
1 Introduction
Today, Terry tapped my handling of a piece of code, which, as the title says, originated from my small handling of a list<t> transformation. First of all, this list<t> converts the true nature of the two sides to a common user class, for example, the user class represents the entity class of the model layer, which is defined as:
// 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; }
}
The account class, then, code the business class at the business object layer, which is defined as:
// 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; }
}
One important thing, then, is how to make a transition between the two, especially, a transformation like list<user> to list<account>, which often happens in the actual operation of the business process. The difference between the two is a design topic, not the object of this article.
2 Original Implementation---think of foreach.
Well, the typical list<t> conversion, the thought of our inherent thinking is the loop, so I don't even think about the following process:
// Release : code10, 2008/10/06
// Author : Anytao, http://www.anytao.com
public List<Account> GetAccounts(List<User> users)
{
List<Account> accounts = new List<Account> ();
foreach (User item in users)
{
Account account = new Account();
account.ID = item.ID;
account.Name = item.FirstName + item.SecondName;
account.Age = item.Age;
accounts.Add(account);
}
return accounts;
}
There is nothing wrong with inherent thinking, and procedures and processes are as memorable as ever. But forgetting is just as important as moving forward, so I forget to do it the easiest way to handle the operation more gracefully.