猿问

c# - 如何在列表中查找带有列的对象#

我叫蒂亚戈!我正在学习 C#。我需要做一个练习,在带有分区(列)的列表中搜索一个对象。


C# 版本 4.5.2 Visual Studio 2017


列表:


alunosMatriculados.Add(new Aluno { matAluno = 2, nomeAluno = "MARIANA DA SILVA", cpfAluno = "111.111.111-12", turmaAluno = "2I", numFaltas = 0 });

alunosMatriculados.Add(new Aluno { matAluno = 3, nomeAluno = "ANA MARIA SILVEIRA", cpfAluno = "111.111.111-13", turmaAluno = "1H", numFaltas = 5 });

alunosMatriculados.Add(new Aluno { matAluno = 4, nomeAluno = "ROBERTO LINS", cpfAluno = "111.111.111-14", turmaAluno = "3H", numFaltas = 1 });

好的。问题是“在用户插入的列表中搜索某些内容”


我试过:


string name;


 Console.WriteLine("Student's name");

    name = Console.ReadLine();


    Console.WriteLine(alunosMatriculados.Find(x => x.nomeAluno.Contains(name)));

varname是学生的名字 nomeAluno是学生的名字(我是巴西人...)


请有人帮助我!


动漫人物
浏览 132回答 2
2回答

慕村225694

这是我的想法:&nbsp; &nbsp; class Aluno&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public int matAluno { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string nomeAluno { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string cpfAluno { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string turmaAluno { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public int numFaltas { get; set; }&nbsp; &nbsp; }&nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var alunosMatriculados = new List<Aluno>();&nbsp; &nbsp; &nbsp; &nbsp; alunosMatriculados.Add(new Aluno { matAluno = 2, nomeAluno = "MARIANA DA SILVA", cpfAluno = "111.111.111-12", turmaAluno = "2I", numFaltas = 0 });&nbsp; &nbsp; &nbsp; &nbsp; alunosMatriculados.Add(new Aluno { matAluno = 3, nomeAluno = "ANA MARIA SILVEIRA", cpfAluno = "111.111.111-13", turmaAluno = "1H", numFaltas = 5 });&nbsp; &nbsp; &nbsp; &nbsp; alunosMatriculados.Add(new Aluno { matAluno = 4, nomeAluno = "ROBERTO LINS", cpfAluno = "111.111.111-14", turmaAluno = "3H", numFaltas = 1 });&nbsp; &nbsp; &nbsp; &nbsp; foreach(var aluno in FindByName(alunosMatriculados, "SIL"))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(aluno.nomeAluno);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Console.ReadLine();&nbsp; &nbsp; }&nbsp; &nbsp; static IEnumerable<Aluno> FindByName( IEnumerable<Aluno> alunos, string partOfName )&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; //TODO error handling&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; //TODO use brazilian culture, if needed&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; alunos&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Where(a => !string.IsNullOrEmpty(a.nomeAluno))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Where(a => a.nomeAluno.Contains(partOfName));&nbsp; &nbsp; }&nbsp; &nbsp; static Aluno FindFirstByName(IEnumerable<Aluno> alunos, string name)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return FindByName(alunos, name)?.FirstOrDefault();&nbsp; &nbsp; }

炎炎设计

这是因为作为结果List<T>.Find返回T,Console.WriteLine并将打印对象.ToString()作为输出。结果是打印出来Aluno.ToString(),默认是类名。我会这样写Aluno a = alunosMatriculados.Find(x => x.nomeAluno.Contains(name));if (a != null)&nbsp;{&nbsp; &nbsp; &nbsp;// ... Whatever you want to do with&nbsp;}else Console.WriteLine("Not found");这样,我们涵盖了可能没有任何项目符合我们的条件的情况。但是,仍然存在可能有多个匹配项的情况,我建议您查找如何使用 LINQ Where,foreach以便您可以打印所有匹配项。
随时随地看视频慕课网APP
我要回答