In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to use the query expression GroupBy in c #, which is very detailed and has a certain reference value. Friends who are interested must finish reading it!
Use cases of basic experimental data:
Student class:
Public class Student {public int StuId {get; set;} public string ClassName {get; set;} public string StudentName {get; set;}}
The setting data are as follows:
List studentList = new List {new Student {ClassName = "soft worker Class 1", StudentName = "Kangba 1", StuId = 1}, new Student {ClassName = "soft worker Class 1", StudentName = "Kangba II", StuId = 2}, new Student {ClassName = "soft worker Class 1", StudentName = "Kangba 3", StuId = 3}, new Student {ClassName = "soft worker Class 2" StudentName = "Kangding Class 1", StuId = 4}, new Student {ClassName = "soft Engineering Class 2", StudentName = "Kangding Class 2", StuId = 5}, new Student {ClassName = "soft Engineering Class 2", StudentName = "Kangding 3", StuId = 6},}
Let's assume that there are a total of six students in the two classes, which are now grouped according to the class.
IEnumerable studentGroup = studentList.GroupBy (s = > s.ClassName)
For example, after calling the GroupBy extension method, the return type is IEnumerable, and IEnumerable represents that the return result can be traversed by foreach, where the generic implementation is IGrouping. According to the generally understood concept of grouping, it can be inferred that string in IGrouping should represent a key, that is, ClassName, then key should correspond to a collection of Student, but how should the code be implemented?
You can foreach studentGroup first.
Foreach (IGrouping item in studentGroup) {}
You can item at this time. Take a look at the prompts.
At this point, it is found that the only property that can be prompted is a key, so how to get the grouped Student collection through item? At this time, we found the second GetEnumerator () method, which shows that item can be foreach, the type is IEnumerator, and the type that can be traversed is Student.
Then you can try item under foreach.
If it turns out to be Student, by inference, now traverse all the data in foreach, and then type it out to have a look.
Foreach (IGrouping item in studentGroup) {Console.WriteLine (item.Key); foreach (var student in item) {Console.WriteLine (student.StudentName);}}
The implementation results are as follows:
So it can be concluded that item is a collection of Student, so why does item still have a key attribute, which seems to be different from the usual collection? the fact is indeed different. Let's take a look at the definition of IGrouping as follows:
Public interface IGrouping: the key obtained by IEnumerable, IEnumerable {/ The key of / /. / [_ _ DynamicallyInvokable] TKey Key {[_ _ DynamicallyInvokable] get;}}
IGrouping's key is stored as its own property, and TElement implements IEnumerable, so when calling foreach to traverse IGrouping, the collection of Student is returned.
This exploration is very interesting, through the artifact vs intelligent prompt and source code implementation finally know the use of GroupBy, and understand why it is used this way.
At the same time, we can also see that polymorphism can be skillfully realized through the interface, which is naturally interesting!
The above is all the contents of the article "how to use the query expression GroupBy in c #". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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: 0
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.