C#:exists或getfirstordefault的替代方案

C#:exists或getfirstordefault的替代方案,c#,collections,.net-3.0,C#,Collections,.net 3.0,我有一节课 class Person { int Age; string Name; } List<Person> pList = new List<Person>(); pList.Exists(p=> p.Name == "testName"); <-- need an alternative for this. 班级人员{ 智力年龄; 字符串名; } List pList=新列表(); pList.Exists(p=>p.Name==“t

我有一节课

class Person {

 int Age;
 string Name;
}

List<Person> pList = new List<Person>();

 pList.Exists(p=> p.Name == "testName");  <-- need an alternative for this.
班级人员{
智力年龄;
字符串名;
}
List pList=新列表();
pList.Exists(p=>p.Name==“testName”) 应该没问题-您只需要处理
p
null
的可能性

bool nameExists = pList.Exists(p => p != null && p.Name == "testName");
或者,确保你的列表不包含任何
null
引用,这可能会让你的所有事情变得更容易

bool nameExists = pList.Any(p=>p.Name == "testName");
或者(如果您不使用
任何
):


你有什么?您是否有数组、列表、IEnumerable或什么?这个系列里面有什么?你想用这些收藏品做什么?显示一些示例输入/输出。
bool nameExists = pList.Select(p=>p.Name).Contains("testName");