Group
You can use the group clause to group query results based on a keyword value.
To achieve a level-1 champion, We should group the champion by country and list the number of champions in each country.
Clause group r by r. Country into g combines all contestants according to the Country attribute and defines a new identifier g,It is used to access the results of the group.
The results of the group clause are sorted by the extended Count () method applied to the grouping results,
If the number of Champions is the same, they are sorted by keywords. the keyword is country, because this is the keyword used by the Group.
The where clause filters data based on at least two groups,
The select clause creates an anonymous type with the Country and Count attributes as the result.
Private static void Grouping () {var countries = from r in Formula1.GetChampions () group r by r. country into g orderby g. count () descending, g. key // if the number of Champions is the same, sort by keyword. the keyword is country, because this is the keyword used by the Group. Where g. count ()> = 2 select new {Country = g. key, Count = g. count ()}; foreach (var item in countries) {Console. writeLine ("{0,-10} {1}", item. country, item. count );}}
To perform the same operation using the extension method, resolve the groupby clause to the GroupBy () method.
In the Declaration of the GroupBy () method, note that it returns the object enumeration that implements the IGrouping interface.
The IGrouping interface defines the Key attribute. Therefore, after calling this method, you can access the Key words of the group:
Public static IEnumerable <IGrouping <TKey, TSource> GroupBy <TSource, TKey> (
This IEnumerable <TSource> source, Func <TSource, TKey> keySelector );
Clause group r by r. Country into g is parsed as GroupBy (r => r. Country), and the group series are returned. Grouping series is first used
Sort by the OrderByDescending () method and then use the ThenBy () method. Then, call the Where () and Select () methods.
private static void Grouping() { var countries = Formula1.GetChampions(). GroupBy(r => r.Country). OrderByDescending(g => g.Count()). ThenBy(g => g.Key). Where(g => g.Count() >= 2).
Select(g => new { Country = g.Key, Count = g.Count() }); foreach (var item in countries) { Console.WriteLine("{0,-10} {1}", item.Country, item.Count); } }
Grouping nested objects
If the group object contains nested objects, you can change the anonymous type created by the select clause.
In the following example, the created country should not only contain the national name and the number of players, but also contain a sequence of names.
This sequence is specified by an internal from in clause that gives the Racers attribute,
The from clause uses the Group Identifier g to obtain all contestants in the group and sorts them by their surnames,
Create a new string based on the name:
private static void GroupingWithNestedObjects() { var countries = from r in Formula1.GetChampions() group r by r.Country into g orderby g.Count() descending, g.Key where g.Count() >= 2 select new { Country = g.Key, Count = g.Count(),
// Before that, it is the same as before. Next, we sort and query the results of each group based on g, as if SQL is nested, then it becomes an anonymous object attribute Racers = from r1 in g orderby r1.LastName select r1.FirstName + "" + r1.LastName}; foreach (var item in countries) {Console. writeLine ("{0,-10} {1}", item. country, item. count); foreach (var name in item. racers) // new attribute of the anonymous object {Console. write ("{0};", name);} Console. writeLine ();}}
Result:
The example of the second nested group is the same as that of the SQL nested group. The result is calculated by the query and then queried again.
Private static void GroupingAndAggregation () {var countries = from c in from r in Formula1.GetChampions () group r by r. country into g select new {Country = g. key, Wins = (from x in g select x. wins ). sum () // pay attention to the call after brackets} // The obtained Key and the anonymous object of the two winning attributes are used as the source orderby c. wins descending // then sort select c; foreach (var item in countries) {Console. writeLine (item );}}
Refer to: http://terryfeng.iteye.com/blog/516126