public static DM.Contest ToDbModel(this XmlModels.Contest xcontest, string problemIdPrefix)
        {
            var contest = new  DM.Contest();

            contest.LastUpdate = DateTime.Now;
            contest.Name       = xcontest.Name;
            var problems = xcontest.Challenge.Problems
                           .Select(p => new DM.Problem().SetName(p.Name).SetAlias(p.Alias));

            var studentDict = new Dictionary <string, DM.Student>();
            Func <string, DM.Student> getStudent = (string name) =>
            {
                if (!studentDict.ContainsKey(name))
                {
                    studentDict[name] = new DM.Student().SetName(name);
                }
                return(studentDict[name]);
            };

            var problms = xcontest.Sessions
                          .SelectMany(s =>
                                      s.Problems.Select(p =>
                                                        new DM.Problem()
                                                        .SetAlias(p.Alias)
                                                        .SetId(problemIdPrefix + p.Id)
                                                        .SetSubmitters(new [] { getStudent(s.Party) })
                                                        .SetSubmissions(p.Runs
                                                                        .TakeWhile(run => run.Accepted == "no")
                                                                        .Concat(p.Runs.Where(run => run.Accepted == "yes").Take(1))
                                                                        .Select(r => new DM.Submission()
                                                                                .SetFromRun(r)
                                                                                .SetContest(contest)
                                                                                .SetSubmitter(getStudent(s.Party))))))
                          .GroupBy(p => p.Alias)
                          .Select(g => new DM.Problem()
                                  .SetContest(contest)
                                  .SetAlias(g.First().Alias)
                                  .SetId(g.First().Id)
                                  .SetSubmissions(g.SelectMany(p => p.Submissions))
                                  .SetSubmitters(g.SelectMany(p => p.Submitters.Select(sp => sp.Student))))
                          .Select(p => p.SetName(problems.First(pr => pr.Alias == p.Alias).Name));

            contest.Problems    = problms.Select(pr => pr.SetSubmissions(pr.Submissions.Select(s => s.SetProblem(pr)))).ToList();
            contest.Submissions = contest.Problems.SelectMany(p => p.Submissions).ToList();
            return(contest);
        }
 public static DM.Submission SetSubmitter(this DM.Submission submission, DM.Student submitter)
 {
     submission.Submitter = submitter;
     return(submission);
 }
 public static DM.Student SetSubmissions(this DM.Student student, IEnumerable <DM.Submission> submissions)
 {
     student.Submissions = submissions.ToList();
     return(student);
 }
 public static DM.Student SetName(this DM.Student student, string name)
 {
     student.Name = name;
     return(student);
 }