List.Select():一般List中的元素是对象的时候,可以通过Selec()获取列表中对象的属性值列表。
1 using System.Collections; 2 using System.Collections.Generic; 3 using System.Linq; 4 using UnityEngine; 5 6 public class Test : MonoBehaviour 7 { 8 /// <summary> 9 /// 所有学生 10 /// </summary> 11 public List<Student> students = new List<Student>(); 12 13 private void Start() 14 { 15 //提取所有学生的姓名和性别 16 foreach (var nameAndGender in ExtractAllStudentNameAndAge()) 17 { 18 Debug.Log(nameAndGender.name +"," + nameAndGender.gender); 19 } 20 21 //提取所有学生的学号 22 foreach (var id in ExtractAllStudentId()) 23 { 24 Debug.Log(id); 25 } 26 27 //每个学生年龄是否大于23 28 foreach (var item in AgeOver23()) 29 { 30 Debug.Log(item); 31 } 32 } 33 34 #region 提取所有学生的姓名和性别 35 36 /*//写法1 37 List<dynamic> ExtractAllStudentNameAndAge() 38 { 39 List<dynamic> allId = students.Select( 40 t => (dynamic)new { t.name, t.gender } 41 ).ToList(); 42 43 return allId; 44 }*/ 45 46 //写法2 47 List<NameAndGender> ExtractAllStudentNameAndAge() 48 { 49 List<NameAndGender> allId = students.Select( 50 t => new NameAndGender(t.name, t.gender) 51 ).ToList(); 52 53 return allId; 54 } 55 #endregion 56 57 #region 提取所有学生的学号 58 59 /*//写法1 60 List<dynamic> ExtractAllStudentId() 61 { 62 List<dynamic> allId = students.Select( 63 t => (dynamic)t.id 64 ).ToList(); 65 66 return allId; 67 }*/ 68 69 //写法2 70 List<int> ExtractAllStudentId() 71 { 72 List<int> allId = students.Select( 73 t => (int)t.id 74 ).ToList(); 75 76 return allId; 77 } 78 #endregion 79 80 #region 每个学生年龄是否大于23 81 82 /*//写法1 83 List<dynamic> AgeOver23() 84 { 85 List<dynamic> allResult = students.Select( 86 t => (dynamic)t.age > 23 87 ).ToList(); 88 89 return allResult; 90 }*/ 91 92 //写法2 93 List<bool> AgeOver23() 94 { 95 List<bool> allResult = students.Select( 96 t => t.age > 23 97 ).ToList(); 98 99 return allResult; 100 } 101 #endregion 102 } 103 /// <summary> 104 /// 学生信息 105 /// </summary> 106 [System.Serializable] 107 public class Student 108 { 109 /// <summary> 110 /// 名字 111 /// </summary> 112 public string name; 113 /// <summary> 114 /// 年龄 115 /// </summary> 116 public int age; 117 /// <summary> 118 /// 学号 119 /// </summary> 120 public int id; 121 /// <summary> 122 /// 性别 123 /// </summary> 124 public Gender gender; 125 126 public override string ToString() 127 { 128 return string.Format("{0},{1},{2},{3}", name, age, id, gender == Gender.Female ? "女" : "男"); 129 } 130 } 131 /// <summary> 132 /// 性别 133 /// </summary> 134 public enum Gender 135 { 136 /// <summary> 137 /// 女性 138 /// </summary> 139 Female, 140 /// <summary> 141 /// 男性 142 /// </summary> 143 Male, 144 } 145 public class NameAndGender 146 { 147 /// <summary> 148 /// 名字 149 /// </summary> 150 public string name; 151 /// <summary> 152 /// 性别 153 /// </summary> 154 public Gender gender; 155 public NameAndGender(string name, Gender gender) 156 { 157 this.name = name; 158 this.gender = gender; 159 } 160 public override string ToString() 161 { 162 return string.Format("{0},{1}", name, gender == Gender.Female ? "女" : "男"); 163 } 164 }View Code
标签:return,name,C#,gender,List,public,Select From: https://www.cnblogs.com/Peng18233754457/p/18040294