All, any, where, firstordefault, average, sum, distinct, union, addrange, removerange, insertrange, and getrange operations of the List

Source: Internet
Author: User

The collection operation discovery is getting richer and richer now. Today's Christmas, we take the commonly used list <t> as an example to quietly read some of the collection operations, which is really convenient to use.

1. merge, insert, delete, and display a set
Addrange (a set): adds the elements of a specified set to the end.
Removerange (the index starting from scratch for the element to be removed, and the number of elements to be removed): removes a certain range of elements.
Insertrange (insert the index of the new element from scratch, a set): insert the set to the specified index. You can also select a range for the set before inserting it.
Getrange (index starting from scratch at the beginning of the range, number of elements in the range): return data in the specified range. This data is a superficial copy, and the superficial copy only contains

Reference of elements in the Set

For example, the Code of program. CS:

 

[C-sharp]View plaincopy
  1. Using system;
  2. Using system. Collections. Generic;
  3. Using system. LINQ;
  4. Using system. text;
  5. Namespace consoleapplist
  6. {
  7. Class Program
  8. {
  9. Static void main (string [] ARGs)
  10. {
  11. String [] stuname = {"aaaa", "BBBB", "CCCC "};
  12. List <string> li = new list <string> (stuname );
  13. Console. writeline ("merge another set :");
  14. Li. addrange (LI );
  15. Foreach (string name in Li)
  16. {
  17. Console. writeline (name );
  18. }
  19. Console. writeline ("/R/n delete operation (specify the index location and number of deletions ):");
  20. Li. removerange (2, 2 );
  21. Foreach (string name in Li)
  22. {
  23. Console. writeline (name );
  24. }
  25. Console. writeline ("/R/n specify the index location and insert another set :");
  26. Stuname = new string [] {"Zhang San", "Li Si", "Wang Wu "};
  27. Li. insertrange (3, stuname );
  28. // Select the specified range of the SET TO BE INSERTED
  29. // Li. insertrange (3, new list <string> (stuname). getrange (1, 2 ));
  30. Foreach (string name in Li)
  31. {
  32. Console. writeline (name );
  33. }
  34. Console. writeline ("/R/N :");
  35. String [] Output = Li. getrange (2, 3). toarray ();
  36. // List <string> output = Li. getrange (2, 3 );
  37. Foreach (string name in output)
  38. {
  39. Console. writeline (name );
  40. }
  41. }
  42. }
  43. }

 

Running result:

Merge another set:
Aaaa
Bbbb
CCCC
Aaaa
Bbbb
CCCC

Delete operation (specify the index location and delete count ):
Aaaa
Bbbb
Bbbb
CCCC

Specify the index location and insert another set:
Aaaa
Bbbb
Bbbb
Zhang San
Li Si
Wang Wu
CCCC

Display data within the specified index range:
Bbbb
Zhang San
Li Si
Press any key to continue...

 

2. Set common judgment, selection, and computing functions
All () determines whether all elements in the sequence meet the conditions.
Any () determines whether any element in the sequence exists or meets the conditions.
Where () returns the elements that meet the conditions. predicate the filter value sequence.
Firstordefault () returns the first element in the sequence. If the sequence does not contain any element, the default value is returned.
Average value of the average () value sequence
Sum () value sequence sum
Distinct () returns non-repeating elements in the sequence
Union () generates the union of two sequences by using the default equal comparator. Remove duplicate elements

For example, program2.cs code:

 

[C-sharp]View plaincopy
  1. Using system;
  2. Using system. Collections. Generic;
  3. Using system. LINQ;
  4. Using system. text;
  5. Namespace consoleapplist
  6. {
  7. Public class student
  8. {
  9. Public string name {Get; set ;}
  10. Public String sex {Get; set ;}
  11. Public int age {Get; set ;}
  12. }
  13. Class Program
  14. {
  15. Static void main (string [] ARGs)
  16. {
  17. Student [] Stu = {New Student {name = "Zhang San", sex = "male", age = 22 },
  18. New student {name = "", sex = "male", age = 33 },
  19. New student {name = "Wang Wu", sex = "female", age = 19 }};
  20. List <student> li = new list <student> (Stu );
  21. // Whether all students are boys
  22. Bool allmale = Li. All (S => S. Sex = "male ");
  23. Console. writeline ("are all students boys?" + allmale );
  24. // Whether boys exist
  25. Bool anymale = Li. Any (S => S. Sex = "male ");
  26. Console. writeline ("/R/n students with boys:" + anymale );
  27. // Displays students older than or equal to 20
  28. Console. writeline ("/R/n shows students older than or equal to 20 :");
  29. Ienumerable <student> query = Li. Where (S => S. age> = 20 );
  30. Foreach (student s in query)
  31. {
  32. Console. writeline ("name:" + S. Name + ", Gender:" + S. Sex + ", age" + S. Age. tostring ());
  33. }
  34. // Returns the first element in the sequence.
  35. Console. writeline ("/R/n returns the first element in the sequence :");
  36. Student sfirstdef = Li. firstordefault <student> ();
  37. Console. writeline ("name:" + sfirstdef. Name + ", Gender:" + sfirstdef. Sex + ", age" + sfirstdef. Age. tostring ());
  38. // Average age of all students
  39. Double avgage = Li. Average <student> (S => S. Age );
  40. Console. writeline ("/R/n average age of all students:" + avgage. tostring ("F2 "));
  41. // Total age of all students
  42. Int avgsum = Li. Sum <student> (S => S. Age );
  43. Console. writeline ("/R/n total age of all students:" + avgsum );
  44. // Return non-repeating elements in the sequence
  45. List <int> ages = new list <int> };
  46. Ienumerable <int> distinctages = ages. Distinct <int> ();
  47. Console. writeline ("/R/n returned sequence of non-repeating elements :");
  48. Foreach (int A in distinctages)
  49. {
  50. Console. Write (a + ",");
  51. }
  52. Console. writeline ();
  53. // Generate the union of two sequences to remove duplicate elements
  54. Console. writeline ("/R/n generates the union of the two sequences, removing the repeated elements :");
  55. Int [] ints1 = {5, 3, 9, 7, 5 };
  56. Int [] ints2 = {8, 3, 6, 4, 4 };
  57. Ienumerable <int> Union = ints1.union (ints2 );
  58. Foreach (INT num in Union)
  59. Console. Write ("{0}", num );
  60. Console. writeline ();
  61. }
  62. }
  63. }

 

Running result:

Whether all students are boys: false

Students with boys: True

Show students older than or equal to 20:
Name: Zhang San, Gender: male, age 22
Name: Li Si, Gender: male, age 33

Returns the first element in the sequence:
Name: Zhang San, Gender: male, age 22

Average age of all students: 24.67

Total Age of all students: 74

Return non-repeating elements in the sequence:
, 33, 27,

Generate the union of the two sequences and remove the repeated elements:
5 3 9 7 8 6 4
Press any key to continue...

All, any, where, firstordefault, average, sum, distinct, union, addrange, removerange, insertrange, and getrange operations of the List

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.