Now I can no longer program C # as it can free me from numerous mechanical codes, it makes writing and reading code more like creating and appreciating art.
Here, we will share with you some previous experiences in learning LINQ.
I. linqpad:
Linqpad is a professional tool for learning LINQ, and also an ide. It provides many instances from linq2sql to linq2xml. linqpad also provides the Il language code of LINQ code, for those who want to study the underlying principles of LINQ, it is definitely a rare thing.
I will not introduce the use of linqpad here. There are many related articles on the garden and the net.
: Http://www.linqpad.net/
Ii. resharper:
Resharper is a powerful plug-in of Visual Studio. It provides excellent support from code analysis to system testing. I believe you will not be unfamiliar with it.
Here we will only look at the resharper function in code refactoring from the perspective of the beginner's LINQ.
Resharper recommends that you use LINQ to write refactoring code. Beginners often need to compare the LINQ code with the traditional C # code, so as to gain a better understanding of LINQ.
If we don't want to write the LINQ program and do not want to query the relevant help documentation, we can directly write the traditional code, such as the foreach method, and then resharper will help us reconstruct it, what we need to do is to learn the code before and after reconstruction.
Here is a picture:
public class Account { public int AId; public List<Holding> Holdings; } public class Holding { public int HId; public List<Transaction> Transactoins; } public class Transaction { public int TId; public List<TaxLot> TaxLots; } public class TaxLot { public double Cost; } public double GetAllTaxLots(List<Account> accounts) { double totalCost = 0.0; foreach(var account in accounts) { foreach(var holding in account.Holdings) { foreach (var transaction in holding.Transactoins) { foreach (var tax in transaction.TaxLots) { totalCost += tax.Cost; } } } } return totalCost; }
The getalltaxlots function calculates the transaction fees of the input account. We use a four-layer foreach loop to see how resharper is restructured:
Let's look at the restructured code:
public double GetAllTaxLots(List<Account> accounts) { return (from account in accounts from holding in account.Holdings from transaction in holding.Transactoins from tax in transaction.TaxLots select tax.Cost).Sum(); }
Isn't it refreshing a lot at once, and you don't even need the getalltaxlots function at all?