Improving coding efficiency in the special topics of Linq -- the first Aggregate method, linqaggregate
We know that linq is a very old thing, and we know that since we use linq, our foreach is much less, however, there is a reality that we use only a few of them in practical applications.
Several methods, this series will lead you to see the linq, okay, not much nonsense, first from the Aggregate mink.
I. Application scenarios
Not long ago, when I was writing a project, I needed to find marketing activities. There were two types of marketing activities: common activities and triggered activities. Because they were stored in two tables, and capture
After that, we need to perform some object storage and other calculations, so we have code like this.
1 namespace ConsoleApplication1 2 {3 class Program 4 {5 static void Main (string [] args) 6 {7 Dictionary <int, List <Marketing> dic = new Dictionary <int, list <Marketing> (); 8 9 // normal activity 10 if (! Dic. containsKey (1) 11 dic [1] = new List <Marketing> (); 12 dic [1]. add (new Marketing () {MarketingID = 1, MarketingName = "normal activity 1"}); 13 dic [1]. add (new Marketing () {MarketingID = 1, MarketingName = "normal activity 2"}); 14 15 // event activity 16 if (! Dic. containsKey (2) 17 dic [2] = new List <Marketing> (); 18 dic [2]. add (new Marketing () {MarketingID = 3, MarketingName = "event Activity 1"}); 19 dic [2]. add (new Marketing () {MarketingID = 4, MarketingName = "event activity 2"}); 20} 21} 22 23 class Marketing24 {25 public int MarketingID {get; set ;} 26 27 public string MarketingName {get; set;} 28} 29}
After a series of operations, I need to flat the data of key = 1 and key = 2 in the dictionary to a list. How can I do this simple calculation ???
Common Practice: first define a List variable and then a foreach.
1 List<Marketing> marketingList = new List<Marketing>();2 foreach (var key in dic.Keys)3 {4 marketingList.AddRange(dic[key]);5 }
If you don't use Aggregate, you will feel that this method is already very good... What about the facts ??? We should have more awesome practices !!!
Awesome practices:
1 var marketingList = dic.Keys.Aggregate(Enumerable.Empty<Marketing>(), (total, next) =>2 {3 return total.Union(dic[next]);4 });
Have you seen how coherent lamda is? There is no fault in the first writing method. Of course, there are Aggregate computing such as Aggregate in many frameworks, such as mongodb.
Aggregate. Let's use ILSpy to see how the Magic Code of Aggregate is implemented.
Ii. Exploring source code
When you see the source code, is there a bright and blind feeling? The so-called Aggregate is actually just the same source code as the "common practice" inside... Aggregate only does
It is a layer of code encapsulation, which improves our development efficiency, right, for example:
We have seen that Aggregate has three reload methods. This article uses the second reload method. The first method looks simpler, right, let alone ILSpy.
This article is about exploration. Thank you for your support ~~~