C # Learning Notes 10

Keywords: C# Attribute SQL

1. Anonymous type: Anonymous type is a new feature of C#3.0. It is a strong type (code defined type automatically declared when the compiler background is born CIL). After declaring and initializing attributes, its attributes are read-through attributes. Multiple declared anonymous variables share the same anonymous type only when the name, order and type of the attribute are identical. Its ToString() method has rewritten the text returned by the attribute name and value.

2. Implicit local variables: When using var to declare local variables, two points should be paid attention to, respectively.

(1) You can clearly know the type declaration on the left from the type on the right side of the assignment "=". For example, new FileInfo("") can clearly know the intensity of the file, and you can use var. But if a method call is on the right, the type should be explicitly declared. It is easy to know the return type without having to go inside the method.

(2) In general, var can be used when using anonymous types, because the type generated by compilation can not be clearly known. However, explicit declarations should be used in standard query operators such as Linq, such as IEnumerable < string > result, which is more readable than var result.

3. Do not modify the collection within the foreach loop.

4. Set initializer: To compile successfully, a set initializer needs to satisfy one of two points, namely

(1) Ideally, a collection should implement the ICollecation < T> interface, so that it can support an Add() method, which can be called by the code generated by the compiler.

(2) In a loose case, a collection should implement the IEnumerable < T> interface, which has one or more Add() methods. Even if the collection type does not implement the ICollecation < T > interface, the Add() method needs to accept parameters compatible with the values specified in the collection initializer.

Note that an anonymous type cannot use a collection initializer directly, because the collection initializer needs to perform a constructor call, but you can't name the constructor at all. There are two ways.

(1) Define a method like the following to create a collection of anonymous types by calling this method, (static List < T > CreateList < T > (T) {return new List < T >();}).

(2) Array processing, such as var items = new [] {...}.

5. Standard query operators: When where() and select() are used, the data are processed vertically and horizontally. In the use of Linq, most of the actions are delayed, so we should pay attention to the need for immediate execution. In. Net4.0, PLinq (parallel Linq) function is added, using the set of AsParallel(), which is a member of the Parallel Enumerable class and an extension method. Parallel queries or filtering of data can then be used.

For example, list. AsParallel (). Where (t=> t. Name. StartWith ("A").

6. When using Linq for ascending sorting, the first sorting method uses OrderBy(), followed by the other sorting using ThenBy(), the descending order is the same.

7.Join and GroupJoin: The former is an inline join of two sets (inner Join equivalent to sql); the latter is a grouping join (realizing one-to-many relationship), that is, the elements of the right set after the left set is grouped.

8. Left Outreach: Using GroupJoin().SelectMany() and DefaultIfEmpty(), you can see the code in the CorporateData.GetLeftInnerData() method.

9. Occasionally, we deal with collections consisting of collections. If we need to double nested traversal to obtain each item, we can use SelectMany() method to process it here, which can turn the collection of collections into a single new collection.

public class CorporateData
{
    public static Department[] Departments = new Department[]
    {
         new Department(){Id = "1",Name = "Ministry of Justice"},
         new Department(){Id = "2",Name = "Information Department"},
         new Department(){Id = "3",Name = "Marketing Department"},
         new Department(){Id = "4",Name = "Finance Department"},
         new Department(){Id = "5",Name = "Administration Department"}
    };
    public static Employee[] Employees = new Employee[]
    {
        new Employee() {No = "01",Name = "Liu Yi",CallName = "Executive director",DepartmentId = "1"},
        new Employee() {No = "01",Name = "Xu er",CallName = "Executive director",DepartmentId = "1"},
        new Employee() {No = "01",Name = "Zhang San",CallName = "Executive director",DepartmentId = "3"},
        new Employee() {No = "01",Name = "Li Si",CallName = "Executive director",DepartmentId = "3"},
        new Employee() {No = "01",Name = "Wang Wu",CallName = "Executive director",DepartmentId = "2"},
        new Employee() {No = "01",Name = "Zhu Liu",CallName = "Executive director",DepartmentId = "2"},
        new Employee() {No = "01",Name = "Yan Qi",CallName = "Executive director",DepartmentId = "4"},
        new Employee() {No = "01",Name = "Mao Ba",CallName = "Executive director",DepartmentId = "4"},
        new Employee() {No = "01",Name = "Huang Jie",CallName = "Executive director",DepartmentId = "4"},
        new Employee() {No = "01",Name = "Into ten",CallName = "Executive director",DepartmentId = "4"}
    };

    //Collection Left External Connection Processing
    public static void GetLeftInnerData()
    {
        var result = Departments.GroupJoin(Employees, d => d.Id, e => e.DepartmentId,
                (d, dEmployees) => new { d.Id, d.Name, Employees = dEmployees })
                .SelectMany(departmentEmployees => departmentEmployees.Employees.DefaultIfEmpty(),
                    (d, e) => new { d.Name, employee = e });
        foreach (var item in result)
        {
            Console.WriteLine("department : {0}, employee : {1}", item.Name, item.employee);
        }
    }

    //Grouping set
    public static void GetGroupBy()
    {
        IEnumerable<IGrouping<string, Employee>> result = Employees.GroupBy(e => e.DepartmentId);
        foreach (IGrouping<string, Employee> item in result)
        {
            Console.WriteLine(item.Key);
            foreach (Employee employee in item)
            {
                Console.WriteLine("\t{0}", employee);
            }
        }
    }
}

public class Department
{
    public string Id;
    public string Name;
}

public class Employee
{
    public string No;
    public string Name;
    public string CallName;
    public string DepartmentId;

    public override string ToString()
    {
        return string.Format("Name={0}, CallName={1}", Name, CallName);
    }
}

-------------------------------------------------------- The above contents are arranged according to the Third Edition of C

Posted by user___ on Fri, 22 Mar 2019 00:15:54 -0700