c # query expressions using the GroupBy

c # query expressions using the GroupBy

Description:

c # achieve IEnumerable <T> class interface provides a number of extension methods, in which the Select, Where as the most common, and almost Sql like syntax is better understood, to meet the most basic needs of daily collection process, but also slightly different part of the bend understand them more, analyze the actual realization of the principle is pretty well understood, this article's how to use the GroupBy.

 

Experimental Example data base:

Student categories:

Copy the code

 public class Student
    {
        public int StuId { get; set; }

        public string ClassName { get; set; }

        public string StudentName { get; set; }
    }

Copy the code

Setting data are as follows:

Copy the code

List<Student> studentList = new List<Student>
            {
                new Student {ClassName = "soft workers class", StudentName = "Kangba a", StuId = 1},
                new Student {ClassName = "soft workers class", StudentName = "Kangba two", StuId = 2},
                new Student {ClassName = "soft workers class", StudentName = "Kangba three", StuId = 3},
                new Student {ClassName = "soft second class workers", StudentName = "Kangding a", StuId = 4},
                new Student {ClassName = "soft second class workers", StudentName = "Kangding two", StuId = 5},
                new Student {ClassName = "soft second class workers", StudentName = "Kangding three", StuId = 6},
            };

Copy the code

We assume that two students in the class a total of six, are now grouped according to class

IEnumerable<IGrouping<string, Student>> studentGroup = studentList.GroupBy(s => s.ClassName);

As the code, after calling GroupBy extension method, the return type IEnumerable <IGrouping <string, Student >>, IEnumerable represents foreach return results may be traversed, wherein the generic implementation is IGrouping <string, Student>, the packet according to the commonly understood the concept can be inferred in IGrouping should be a key string representative that ClassName, it should be is a Student of the corresponding set of key, but code should be how to achieve it?

Foreach can first look studentGroup

 

  foreach (IGrouping<string, Student> item in studentGroup)
            {
                
            }

 

This time may item. It and see message

At this time we found out the property can only be prompted only one key, then how to get through the item Student grouped collection of it? It found the second time that the GetEnumerator () method, which can be described foreach item, the type of IEnumerator <Student>, the types described may be traversed to Student

Foreach can then try the next item

If shown, is really Student, by inference through all the data now in foreach then played look

Copy the code

foreach (IGrouping<string, Student> item in studentGroup)
            {
                Console.WriteLine(item.Key);
                foreach (var student in item)
                {
                    Console.WriteLine(student.StudentName);
                }
            }

Copy the code

Execution results are as follows:

It can be concluded that item is a Student collection, then why there is a key item attribute it, like usual collection is not the same, the fact that it was not the same, we look IGrouping defined as follows:

Copy the code

public interface IGrouping<out TKey, out TElement> : IEnumerable<TElement>, IEnumerable
  {
    /// <summary>
    /// Get <see cref = "T: System.Linq.IGrouping`2" /> key.
    /// </summary>
    /// 
    /// <returns>
    /// <see cref = "T: System.Linq.IGrouping`2" /> key.
    /// </returns>
    [__DynamicallyInvokable]
    TKey Key { [__DynamicallyInvokable] get; }
  }

Copy the code

The key is IGrouping as their own property to store up, TElement is to achieve a IEnumerable <TElement>, so when calling foreach traversal IGrouping return, that is a collection of the Student

 

This exploration is very interesting, intelligent tips and source code artifacts vs through the use GroupBy finally know, and understand why such use.

See also the interface to be implemented by smart polymorphism, which naturally is infinite wit!

 

 

Guess you like

Origin blog.csdn.net/cxu123321/article/details/93768004