As a part of LINQ, LINQ to Objects is a syntax for querying object sets. First, let's take a look at the architecture of LINQ, so that we can have a general understanding of LINQ .. // String userName = "James"; int age = 29; string [] userList = new string [] {"a", "B", "c "}; foreach (string user in userList) {Console. writeLine (user) ;}// var userName = "mcgrady"; var age = 29; var userList = new [] {"a", "B ", "c"}; foreach (var user in userList) {Console. writeLine (user );}Anonymous type
The anonymous type is a new feature of C #. Generally, the type object we create is created by keeping up with the class name through the new Keyword, but the anonymous type does not need to be specified, instead, the compiler automatically creates a type name for us without telling us what the type name is. The anonymous type is also called the intra-row type. It is usually used with the var keyword. The Demo code is as follows.
var data = new { UserName = "mcgrady", Age = "29" };Console.WriteLine("UserName:{0},Age:{1}", data.UserName, data.Age);Extension Method
The extension method allows us to perform some extension operations on CLR or custom types without source code. At the same time, it is also the basis for learning LINQ to Objects. In addition, the extension method can only be defined in static methods of static classes. If the extension method name cannot be the same as the original method name, the extension method will become invalid. The Demo code is as follows.
Public static class StringExtension {// <summary> // convert the current string to int /// </summary> /// <param name = "str"> </ param> /// <param name = "result"> </param> /// <returns> if the result is successful, the string is returned in the int format, returns the default value </returns> public static int ToInt (this string str, int result =-110) {int value; if (int. tryParse (str, out value) {return value;} else {return result ;}}// call string str = "111"; int result = str. toInt ();Object initializer
Before we can create an object, we need to take the following two steps: first, create an object and assign values to each field. With the object initializer, tasks that require several lines of code can be completed by turning them into one line of code, which simplifies the code and makes the code more elegant. The Demo code is as follows.
// Originally written as Person person = new Person (); person. userName = "James"; person. age = 28; // the current format is Person person = new Person {UserName = "James", Age = 28}; Console. writeLine ("UserName: {0}, Age: {1}", person. userName, person. age );Set Initiator
Using the set initializer, the compiler will automatically generate the Add insert operation for us, provided that the Set implements the IEnumerable interface. Like the object initializer, using the set initiatorto enhance code readability reduces the amount of code we write. The Demo code is as follows.
// List <Person> personList = new List <Person> (); personList. add (new Person {UserName = "Wade", Age = 28}); personList. add (new Person {UserName = "Bosh", Age = 25}); personList. add (new Person {UserName = "James", Age = 29}); // write it now (use the set initiator) list <Person> newPersonList = new List <Person> {new Person {UserName = "Wade", Age = 28}, new Person {UserName = "Bosh", Age = 25 }, new Person {UserName = "James", Age = 29 }};Lambda expressions
Lambda expressions can be easily understood as follows: It simplifies the Code required to create a delegated instance based on the anonymous method. A later article will specifically summarize lambda expressions. The syntax format is: (parameter list) => expressions or statement blocks. Below is a simple example.
// Lambda expression var selectionPerson = newPersonList. where (p => p. age> 25 ). select (p => p. userName. toUpper (); foreach (var item in selectionPerson) {Console. writeLine (item );}