Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在列表中查找类值_C# - Fatal编程技术网

C# 在列表中查找类值

C# 在列表中查找类值,c#,C#,用户将输入带有姓名、年龄和地址的Lis,即 PeopleList.Add(new People { Name = name, Age = age, Address = address }); 完成PeopleList后,用户可以搜索姓名,并查看年龄和地址 我在如何在PeopleList中搜索特定的名称方面遇到问题 string searchName = Console.ReadLine(); if (PeopleList.Contains(new Peopl

用户将输入带有
姓名
年龄
地址
Lis
,即

PeopleList.Add(new People { 
  Name    = name, 
  Age     = age, 
  Address = address 
});
完成
PeopleList
后,用户可以搜索
姓名
,并查看
年龄
地址

我在如何在
PeopleList
中搜索特定的
名称方面遇到问题

string searchName = Console.ReadLine();

if (PeopleList.Contains(new People {
    Name = searchName
})) {
    //Display name, age, address here
} else {
    Console.WriteLine("Name not found");
}

如果存在第一个元素,请使用Linq
FirstOrDefault()
确定第一个元素

string searchName = Console.ReadLine();
People result = PeopleList.FirstOrDefault(x => x.Name == searchName);
if (result != null)
{
    Console.WriteLine("name: {0}, age: {1}, address:{2}", result.Name, result.Age, result.Address);
}
else
{
    Console.WriteLine("Name not found");
}
您可以尝试在Linq的帮助下查询集合

如果您不想显示找到的第一个项目而是所有项目,您可以
加入它们:

string result = string.Join(Environment.Newline, PeopleList
  .Where(item => item.Name == searchName)
  .Select(found => $"Name: {found.Name}; Age: {found.Age}; Address: {found.Address}")
);

result = string.IsNullOrEmpty(result) 
  ? "Name not found" 
  : result;

Console.Write(result);
发生了什么事
Contains
的签名是
公共静态bool Contains(…)并且它只给你一个是/否的答案。为了显示匹配对象,您需要返回匹配对象的方法之一,而不仅仅是找到匹配对象时返回是/否。这些方法包括
Where
First
FirstOrDefault
Single
SingleOrDefault
,等等


注意:该类应称为
Person
,因为它表示关于一个人的数据,而不是关于多个人的数据

Person person;
List<Person> people;

测验 密码 输出
使用System.Linq
PeopleList.Find(p=>p.Name==searchName)
。多个同名条目如何?由于您没有使用linq标记,您是否考虑过在集合上使用循环?和名字比较?不太合适,但要小心!当您在此行中使用contains方法时:
PeopleList.contains(新人员{…
您为它提供了一个全新的
实例。由于您没有指定比较方法,contains将比较每个实例与新实例的相等性。它不会比较变量
名称
!此语句将始终返回false
Person person;
List<Person> people;
var searchName = Console.ReadLine();
var results = PeopleList
    .Where(x => x.Name.Contains(searchName, StringComparison.InvariantCultureIgnoreCase))
    .ToList();
if (results.Any())
{
    foreach (var person in results)
    {
        Console.WriteLine($"Found: {person.Name}");
    }
}
else
{
    Console.WriteLine("Name not found");
}
class Person { public string Name { get; set; } }
public static void Main(string[] args)
{
    var PeopleList = new[] { new Person { Name = "marneee" }, new Person { Name = "Mark" } };
    while (true)
    {
        var searchName = Console.ReadLine();
        var results = PeopleList
            .Where(x => x.Name.Contains(searchName, StringComparison.InvariantCultureIgnoreCase))
            .ToList();
        if (results.Any())
        {
            foreach (var person in results)
            {
                Console.WriteLine($"Found: {person.Name}");
            }
        }
        else
        {
            Console.WriteLine("Name not found");
        }
    }
}
// .NETCoreApp,Version=v3.0
mar
Found: marneee
Found: Mark
marn
Found: marneee