Veikedo

[C# interview] students and courses

Dec 7th, 2020
507
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.96 KB | None | 0 0
  1. // Найдите любые ошибки/улучшения в следующем фрагменте кода
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public static class Enumerable
  6. {
  7.     public class StudentAndCourse
  8.     {
  9.         public Student Student { get; set; }
  10.         public Course Course { get; set; }
  11.     }
  12.  
  13.     public class Student
  14.     {
  15.         public string Name { get; set; }
  16.         public int? Course { get; set; }
  17.     }
  18.  
  19.     public class Course
  20.     {
  21.         public int? CourseID { get; set; }
  22.         public string Namе { get; set; }
  23.     }
  24.  
  25.     public static IEnumerable<StudentAndCourse> Do(IEnumerable<Student> students, IEnumerable<Course> courses)
  26.     {
  27.         if (students.Count() == 0)
  28.             return new List<StudentAndCourse>();
  29.         var result = new List<StudentAndCourse>();
  30.         foreach (var student in students)
  31.         {
  32.             result.Add(new StudentAndCourse()
  33.             {
  34.                 Student = student,
  35.                 Course = courses.Single(d => d.CourseID == student.Course)
  36.             });
  37.         }
  38.  
  39.         return result;
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment