About Linq Statements

 Linq statements are a set of methods that allow filtering, sorting, and grouping operations to be performed on collections with minimal code. Linq stands for Language Integrated Query. It is the name of a set of techniques based on integrating query functionality into the C# language. While the Linq approach is not language-integrated, there is a declarative, language-integrated query snytax.

  array

  The most basic enumerable item is an array, which can store a fixed number of typed elements.

  int[] numbers = new int[3] { 1, 2, 3};

  The above code creates an integer array holding 3 values ​​and initializes the array with the values ​​1, 2 and 3. Assign the array to a variable of type int[] named numbers. Each element of the array can be accessed by index or enumeration value.

   int fistNumber = numbers[0]; //1
  foreach( int number in numbers){
   
   
     Console.WriteLine(number); // 1,2,3
}

 list

 A common type of enumeration is the list, which, unlike arrays, is variable in size.

var number = new list<int>{1,2,3};

 In the above example, a list of integers is created and initialized with the three values ​​1,2,3. Use the keyword var to define the type of the number. With var, we let the compiler decide which type to use. When we assign an object of type List<int> to a number, the type of the variable will be List<int>. These values ​​can also be accessed by indexing and they can also be enumerated.

 var    FirstNumber = number[0];     //1
froeach(var numbers in number){
   Console.WriteLine(numbers); // 1,2,3
}

  gather

Sets contain unique elements, for example, the HashSet class provides high-performance set operations (such as UnionWith, IntersectWith, etc.)

  var number = new HashSet<int>{1,2,3}

 Unlike the previous two arrays and lists, you cannot access the elements of a collection by its index. There is no such concept as an element index in a hashset. However, it is still possible to enumerate the values ​​stored in the collection.

  foreach( int number in numbers){
   
   
  Console.WriteLine(number); //1,2,3
}
While all enumerable types have different purposes and methods, they share one characteristic. They all implement the same interface, IEnumerable<T>, in Net, there is a declarative language construct in C# that makes it easy to deal with enum items, like the foreach loop above.

  Another note: The interface IEnumberable<T> is a generic interface. This means that enums are strongly typed, but concrete types can be defined in code. Strong typing means that it can only hold elements of the specified type (or can implicitly convert elements of that type). T is the template type and is the order in which the specified types are placed.

  TypeList<T> is also a generic type. The list can be used for any type (<T>). However, you have to choose the fixed type in your code. For example <int> (list of integers) is IEnumerable<int> (enumerable of integers) can add int values ​​to the list. You can also add byte values ​​to the list (because they can be automatically converted to int, i.e. implicit conversion, without loss). However, you cannot add strings or long values ​​to the list. The former cannot be cast to integers at all, the latter can be converted to integers with possible loss of data, and this is never done automatically.

   filter data

In many cases, a dataset must be filtered, a dataset is a collection of objects ( IEnumerable<T> ), and a climbing predicate is an expression or function that can decide whether to keep an item. If the predicate matches, keep the item. If not, you try to drive it away. Example below: Keep only positive values ​​from a list of numbers. The first way to filter the data is to create a new empty list, then enumerate the numbers and add the positive values ​​to the new list.

The above successfully created a filter method KeepositiveNumbers, in this method, we can pass an arbitrary list of numbers as input and only get a list of positive numbers. This method accepts any IEnumerable<int>, so this method uses an array, list, or other data structure that implements the IEnumerable<T> interface. To be added. .

Guess you like

Origin blog.csdn.net/weixin_44591600/article/details/124268444
Recommended