[C#] List 尋找/搜尋符合的條件 (Find相關)

  • 17342
  • 0
  • C#
  • 2019-09-23

[C#]List 尋找符合的條件 (Find相關)

 

尋找List中是否有自己想要的值

覺得很好用~但有時候沒睡飽,又會不小心忘記,所以筆記一下XD

List<OhMyGod> listTest = new List<OhMyGod>();
//是否存在=> 存在回傳true
bool isExists = listTest.Exists(x => x.name == "cat");

//符合條件的第一個index,找不到傳回 -1
int index = listTest.FindIndex(x => x.name== "cat");

//符合條件的第一個object,找不到傳回 null
OhMyGod objMyGod  = listTest.Find(x => x.name.Contains("cat"));

//符合條件的所有objects
List<OhMyGod>  listFind = listTest.FindAll(x => x.name.Contains("cat"));

 

 

參考链接:https://blog.csdn.net/wangzl1163/article/details/72730390

 


與每個人,一起分享所學到,所用到的,

若有錯誤,請您不吝指教,謝謝大家。