【问题标题】:Convert lambda expression into linq c#将 lambda 表达式转换为 linq c#
【发布时间】:2018-05-21 19:26:52
【问题描述】:

我有学生名单和讲师名单,我用双 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;
            }
        }
    }
}

【问题讨论】:

  • 您是在问如何将这段代码翻译成查询语法?
  • = new List(); 没用。您在下面重新分配了两行。
  • 该代码不仅已经是 linq,而且如果您的意思是您希望使用查询语法编写它,那么查询语法也使用 lambdas。
  • @JustGalgaldas - 不在 cmets 中!!!编辑问题以添加此信息。

标签: c# linq lambda


【解决方案1】:

我认为您尝试的示例不起作用,因为它返回 IEnumerable&lt;Student&gt; 而您的方法应该返回 List&lt;Student&gt;

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

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

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

另一种方法是将返回值转换为List&lt;Student&gt;

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

当然还有一些潜在的问题,比如linkedListlecturer 是否为null,或者text 中没有空格(尝试访问索引时会得到IndexOutOfRangeException 1)。此外,您可以在讲师姓名数组上使用Contains 方法来简化您的where 条件:

您可以通过以下方式解决这些问题:

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

【讨论】:

    【解决方案2】:

    这与您的问题的输出完全相同,仅使用 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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多