[Development story] The second time: LINQ comes, forget foreach first

Source: Internet
Author: User
Tags foreach

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.