Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.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#,假设我有以下课程: public class Teacher { public Teacher(string teacherName) { TeacherName = teacherName; StudentNames = new List<string>(); } public string TeacherName { get; set; } public List<string> Student

假设我有以下课程:

public class Teacher
{
    public Teacher(string teacherName)
    {
        TeacherName = teacherName;
        StudentNames = new List<string>();
    }

    public string TeacherName { get; set; }
    public List<string> StudentNames { get; set; }
}
公共课堂教师
{
公共教师(字符串教师姓名)
{
教师姓名=教师姓名;
StudentNames=新列表();
}
公共字符串教学器名称{get;set;}
公共列表学生名{get;set;}
}
我有以下代码:

List<Teacher> teachersList = new List<Teacher>();

Teacher teacherOne = new Teacher("Mrs. Smith");
teacherOne.StudentNames.Add("Johnny");
teacherOne.StudentNames.Add("Samantha");

Teacher teacherTwo = new Teacher("Mr. Andrews");
teacherTwo.StudentNames.Add("Johnny");
teacherTwo.StudentNames.Add("Chris");
teacherTwo.StudentNames.Add("Samantha");

Teacher teacherThree = new Teacher("Ms. Cunningham");
teacherThree.StudentNames.Add("Becky");
teacherThree.StudentNames.Add("Rachel");
teacherThree.StudentNames.Add("Johnny");

Teacher teacherFour = new Teacher("Mr. Pearson");
teacherFour.StudentNames.Add("Samantha");
teacherFour.StudentNames.Add("Rachel");
teacherFour.StudentNames.Add("Kevin");

teachersList.Add(teacherOne);
teachersList.Add(teacherTwo);
teachersList.Add(teacherThree);
teachersList.Add(teacherFour);
List teachersList=newlist();
教师一名=新教师(“史密斯夫人”);
teacherOne.StudentNames.Add(“约翰尼”);
teacherOne.StudentNames.Add(“Samantha”);
教师教师2=新教师(“安德鲁斯先生”);
教师二。学生姓名。加上(“约翰尼”);
教师二。学生姓名。加上(“克里斯”);
teacherTwo.StudentNames.Add(“Samantha”);
教师教师三=新教师(“坎宁安女士”);
教师三。学生姓名。加上(“贝基”);
教师三。学生姓名。加上(“瑞秋”);
教师三。学生姓名。加上(“约翰尼”);
教师四=新教师(“皮尔森先生”);
teacherFour.StudentNames.Add(“Samantha”);
教师四。学生姓名。加上(“瑞秋”);
教师四。学生姓名。加上(“凯文”);
teachersList.Add(teacherOne);
教师列表。添加(教师2);
教师列表。添加(教师三);
教师列表。添加(教师四);
我想把约翰尼和萨曼莎都列在学生名单上的老师拉过来。这意味着只有教师一和教师二应该返回,而不是教师三和教师四,因为他们只有一个或另一个。我尝试了以下方法,但由于它进行部分匹配的方式,它将所有四位老师都拉了回来:

List<string> matchingStudents = new List<string>();
matchingStudents.Add("Johnny");
matchingStudents.Add("Samantha");
return teachersList.Where(teacher => teacher.StudentNames.Any(studentName => matchingStudents.Any(studentName2 => studentName2.Equals(studentName)))).ToList();
List matchingStudents=new List();
配对学生。添加(“约翰尼”);
matchingStudents.Add(“Samantha”);
返回teachersList.Where(teacher=>teacher.StudentNames.Any(studentName=>matchingStudents.Any(studentName2=>studentName2.Equals(studentName))).ToList();
我觉得我错过了一些东西,也许我不得不做一些与十字路口或工会。有什么想法吗?

你可以用

return teachersList
    .Where(teacher => matchingStudents.All(teacher.StudentNames.Contains))
    .ToList();
如果要忽略此情况,则需要更多的代码:

return teachersList
    .Where(t => matchingStudents.All(s => t.StudentNames.Contains(s, StringComparer.OrdinalIgnoreCase)))
    .ToList();
你可以用

return teachersList
    .Where(teacher => matchingStudents.All(teacher.StudentNames.Contains))
    .ToList();
如果要忽略此情况,则需要更多的代码:

return teachersList
    .Where(t => matchingStudents.All(s => t.StudentNames.Contains(s, StringComparer.OrdinalIgnoreCase)))
    .ToList();

这一切都是我所缺少的。谢谢大家!@LOL.NO.:不仅仅是
所有的
,这是一种透视问题。我们有时需要从另一个方向思考。你不希望老师的学生有名字。如果您将注意力集中在搜索的姓名列表上,则必须在教师的学生列表中找到姓名。如果您注意到,您将了解使用哪种方法。@LOL.NO。将
matchingStudents
转换为
HashSet
而不是
列表
可以更有效地解决不区分大小写的问题,因为您可以将
StringComparer.OrdinalIgnoreCase
传递给它的构造函数。啊,是的
teacher.StudentNames
取而代之的是,抱歉没有想到all()这绝对是我所缺少的。谢谢大家!@LOL.NO.:不仅仅是
所有的
,这是一种透视问题。我们有时需要从另一个方向思考。你不希望老师的学生有名字。如果您将注意力集中在搜索的姓名列表上,则必须在教师的学生列表中找到姓名。如果您注意到,您将了解使用哪种方法。@LOL.NO。将
matchingStudents
转换为
HashSet
而不是
列表
,可以更有效地解决不区分大小写的问题,因为您可以将
StringComparer.OrdinalIgnoreCase
传递给它的构造函数。啊,是的
老师。学生姓名
取而代之的是,对不起,我没有考虑