Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How LINQ compares foreach horizontally

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how LINQ compares foreach horizontally. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

1 introduction

First of all, let's take a look at the true face of both sides of the List transformation. Take a common user class as an example, the User class represents the entity class of the Model layer, which is defined as:

/ / Release: code10, 2009-05-06 / / Author: Anytao, http://www.51cto.com public class User {public int ID {get; set;} public string FirstName {get; set;} public string SecondName {get; set;} public int Age {get; set;}}

On the other hand, the Account class codes the business class of the Business Object layer, which is defined as:

/ / Release: code10, 2009-05-06 / / Author: Anytao, http://www.51cto.compublic class Account {public int ID {get; set;} public string Name {get; set;} public int Age {get; set;}}

Then an important thing is how to complete the conversion between the two. In particular, conversions such as List to List often occur in the actual operation of business processing. The difference between the two belongs to the topic of design and is not the object of this article.

2 the original implementation-foreach when I think of it

Well, in a typical List transformation, what we inherently think of is a loop, so I implemented the following process without even thinking about it:

/ / Release: code10, 2009-05-06 / / Author: Anytao, http://www.51cto.com public List GetAccounts (Listusers) {List accounts = new List (); 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 the inherent thinking, and the procedures and handling are as memorable as ever. However, forgetting is as important as moving forward, so I forgot to deal with this operation more elegantly in the easiest way.

3 the way to improve-LINQ is really here

When LIQN comes, don't be stingy with your weapons. Elegance and simplicity are always worth pursuing for the same code requirements, so code that looks like a classic can actually be better:

/ / Release: code10, 2009 public List GetAccounts 06 / Author: Anytao, http://www.51cto.com public List GetAccounts (Listusers) {var result = from item in users select new Account {ID = item.ID,Name = item.FirstName + item.SecondName, Age = item.Age}; return result.ToList ();}

With just a little improvement, the "smart" transformation achieved in LINQ looks more beautiful, doesn't it?

This is the end of the article on "how LINQ compares foreach horizontally". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 264

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report