Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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
将lambda表达式转换为linq c#_C#_Linq_Lambda - Fatal编程技术网

将lambda表达式转换为linq c#

将lambda表达式转换为linq c#,c#,linq,lambda,C#,Linq,Lambda,我有学生名单和讲师名单,我用双重foreach语句编写了代码 有没有办法用Lambda表达式简化这段代码 public void GetLecturersWorkloadStatistics(List<Student> studentList, List<Lecturer> lecturerList) { foreach (Lecturer lecturer in lecturerList) { foreach (Student stude

我有学生名单和讲师名单,我用双重foreach语句编写了代码

有没有办法用Lambda表达式简化这段代码

public void GetLecturersWorkloadStatistics(List<Student> studentList, List<Lecturer> lecturerList)
{
    foreach (Lecturer lecturer in lecturerList)
    {
        foreach (Student student in studentList)
        {
            if (lecturer.ModuleName == student.ModuleName && lecturer.LastName == student.LecturerLastName &&
                lecturer.FirstName == student.LecturerFirstName)
            {
                lecturer.Credits = lecturer.Credits + lecturer.ModuleValueInCredits;
            }
        }
    }
}
public void get讲师工作负载统计信息(列出studentList,列出讲师列表)
{
foreach(讲师列表中的讲师)
{
foreach(学生列表中的学生)
{
如果(讲师.ModuleName==student.ModuleName&&讲师.LastName==student.讲师LastName)&&
讲师.FirstName==学生.讲师FirstName)
{
讲师.Credits=讲师.Credits+讲师.ModuleValueInCredits;
}
}
}
}

我认为您尝试的示例不起作用,因为它返回一个
IEnumerable
,而您的方法应该返回一个
列表

您还缺少将
&
子句组合在一起所需的一些括号,因此它们由
|
运算符分隔

解决此问题的一种方法是将方法的返回类型更改为
IEnumerable
,并在
&&
子句周围添加一些括号:

public IEnumerable<Student> GetStudentBySelectedLecturer(List<Student> linkedList,
    string text)
{
    var lecturerInformation = text.Split(' ');

    return from stud in linkedList
        where (stud.LecturerFirstName == lecturerInformation[0] &&
                stud.LecturerLastName == lecturerInformation[1]) ||
                (stud.LecturerFirstName == lecturerInformation[1] &&
                stud.LecturerLastName == lecturerInformation[0])
        select stud;
}

这与问题的输出完全相同,仅使用lambda表达式

    studentListBySelectedLecturer = (from stud in linkedList
              where stud.LecturerFirstName == lecturerInformation[0] &&
              stud.LecturerLastName == lecturerInformation[1] ||
              stud.LecturerFirstName == lecturerInformation[1] &&
              stud.LecturerLastName == lecturerInformation[0]
              select stud).ToList();

    return studentListBySelectedLecturer;

您是否在询问如何将此代码转换为查询语法?=new List();没用。您将在下面两行重新分配它。不仅代码已经是linq,而且如果您想使用查询语法编写它,那么查询语法也使用lambdas。@justgaldas-不在注释中!!!编辑问题以添加此信息。
public IEnumerable<Student> GetStudentBySelectedLecturer(List<Student> students,
    string lecturer)
{
    if (students == null || lecturer == null) return null;

    var lecturerName = lecturer.Split(' ');

    return from student in students
        where lecturerName.Contains(student.LecturerFirstName) &&
              lecturerName.Contains(student.LecturerLastName)
        select student;
}
    studentListBySelectedLecturer = (from stud in linkedList
              where stud.LecturerFirstName == lecturerInformation[0] &&
              stud.LecturerLastName == lecturerInformation[1] ||
              stud.LecturerFirstName == lecturerInformation[1] &&
              stud.LecturerLastName == lecturerInformation[0]
              select stud).ToList();

    return studentListBySelectedLecturer;