简体   繁体   中英

Convert lambda expression into linq c#

I have List of students and List of lecturers and I wrote code with double foreach statements.

Is there any way of simplifying this code using Lambda expressions?

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;
            }
        }
    }
}

I think the example you're trying doesn't work because it's returning an IEnumerable<Student> and your method is supposed to return a List<Student> ;

You're also missing some parenthesis needed to group your && clauses together, so they're separated by the ||operator.

One way to solve this would be to change the return type of your method to IEnumerable<Student> , and to add some parenthesis around your && clauses:

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;
}

Another way would be to cast the return value to a List<Student> :

public List<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).ToList();
}

Of course there are still some potential problems, like if either linkedList or lecturer is null , or if there is no space in text (you would get an IndexOutOfRangeException when trying to access index 1 ). Also, you can use the Contains method on the array of lecturer names to simplify your where condition:

You could address these by doing something like:

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;
}

This is the exact same output of your issue, only using lambda expressions.

    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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM