Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我目前正在使用“.contains”方法来搜索列表并获取匹配值,例如
清单 1:
dog bark cat meow lion raw
例如
if (List1.Contains("dog")) { // Return the value of this list item e.g. "dog bark" }
听起来像你想要的:
var match = list.FirstOrDefault(x => x.Contains("dog")); if (match != null) { Console.WriteLine(match); }
或显示所有匹配项:
foreach (var match in list.Where(x => x.Contains("dog")) { Console.WriteLine(match); }