在c#中比较List的更好方法

use*_*567 6 c# list

我有两个名单说

List<string> names; and List<Student> stud;
Run Code Online (Sandbox Code Playgroud)

学生班有3处房产

ID
Name
Section
Run Code Online (Sandbox Code Playgroud)

现在我想循环List<string>并比较每个项目与Name属性,List<Student>并希望执行操作,如果它们不相等

我尝试循环遍历名称并将每个值与stud进行比较.

但我认为必须有一些更好的方法,LINQ或者我应该使用YIELD.

谢谢

Jon*_*eet 6

从您的描述中不是很清楚,但如果您想要"所有名字不在列表中的学生",您肯定可以使用LINQ:

var studentsWithoutListedNames = stud.Where(s => !names.Contains(s.Name));
foreach (var student in studentsWithoutListedNames)
{
    // Whatever...
}
Run Code Online (Sandbox Code Playgroud)