public StudentProjectGroups(Student stud, ProjectGroup proj)
 {
     Student      = stud;
     ProjectGroup = proj;
 }
 public StudentProjectGroups(Student stud, ProjectGroup proj)
 {
     Student = stud;
     ProjectGroup = proj;
 }
        public ActionResult MakeProject(HttpPostedFileBase file)
        {
            if (!IsAuthenticated())
            {
                return RedirectToAction("Index", "Main");
            }

            string path = null;

            List<Student> students = new List<Student>();
            if(Request.Form["projectName"].Count() == 0)
                return RedirectToAction("Error", "Main", new { errorMessage = "Er is geen projectnaam ingevuld" });
            if (file != null)
            {
                var fileName = Path.GetFileName(file.FileName);
                path = AppDomain.CurrentDomain.BaseDirectory + "upload\\" + fileName;
                file.SaveAs(path);

                // Read the CSV file data
                StreamReader sr = new StreamReader(path);
                string line = sr.ReadLine();
                string[] value = line.Split(';');
                DataTable dt = new DataTable();
                DataRow row;
                int columnCount = 0;
                foreach (string dc in value)
                {
                    if (dc.Equals("FirstName") || dc.Equals("LastName") || dc.Equals("StudentNr") || dc.Equals("Year") || dc.Equals("ProjectGroup"))
                    {
                        dt.Columns.Add(new DataColumn(dc));
                        columnCount++;
                    }
                    else
                        return RedirectToAction("Error", "Main", new { errorMessage = "Één kolom header is niet correct" });
                }
                if (columnCount != 5)
                    return RedirectToAction("Error", "Main", new { errorMessage = "Het aantal kolommen is niet correct" });
                while (!sr.EndOfStream)
                {
                    value = sr.ReadLine().Split(';');
                    if (value.Length == dt.Columns.Count)
                    {
                        row = dt.NewRow();
                        row.ItemArray = value;
                        dt.Rows.Add(row);
                    }
                }

                Project currentProject = new Project(Request.Form["projectName"], null, new DateTime(2014, 1, 1), new DateTime(2014, 1, 1), null);
                ProjectGroup currentGroup = null;
                Student currentStudent = null;
                List<ProjectGroup> groupsToAdd = new List<ProjectGroup>();
                List<Student> studentsToAdd = new List<Student>();
                var myEnumerable = dt.AsEnumerable();
                foreach (var item in myEnumerable)
                {
                    string pgroup = item.Field<String>("ProjectGroup");
                    int studnr = int.Parse(item.Field<String>("StudentNr"));

                    currentStudent = _db.Students.Find(studnr);

                    //var groupModel = from r in _db.ProjectGroups
                    //                 where r.ClassCode == pgroup
                    //                 select r;
                    //dbGroup = groupModel.FirstOrDefault();

                    if (currentGroup == null)
                    {
                        ProjectGroup newGroup = new ProjectGroup(pgroup, currentProject);
                        groupsToAdd.Add(newGroup);
                        currentGroup = newGroup;
                    }
                    else
                    {
                        if (!currentGroup.ClassCode.Equals(pgroup))
                        {
                            ProjectGroup newGroup = new ProjectGroup(pgroup, currentProject);
                            currentProject.ProjectGroups.Add(newGroup);
                            groupsToAdd.Add(newGroup);
                            currentGroup = newGroup;
                        }
                    }

                    if (currentStudent == null)
                    {
                        Student newStudent = new Student(
                            int.Parse(item.Field<String>("StudentNr")),
                            item.Field<String>("FirstName"),
                            item.Field<String>("LastName"),
                            int.Parse(item.Field<String>("Year")),
                            null); // mentor

                        studentsToAdd.Add(newStudent);
                        currentStudent = newStudent;
                    }

                    if (currentStudent != null && currentGroup != null)
                    {
                        StudentProjectGroups spg = new StudentProjectGroups(currentStudent, currentGroup);
                        currentStudent.StudentProjectGroup.Add(spg);
                        if (currentGroup.StudentProjectGroup == null)
                            currentGroup.StudentProjectGroup = new List<StudentProjectGroups>();
                        currentGroup.StudentProjectGroup.Add(spg);
                    }
                }

                _db.Projects.Add(currentProject);

                foreach (Student stud in studentsToAdd)
                {
                    _db.Students.Add(stud);
                }
                foreach (ProjectGroup group in groupsToAdd)
                {
                    _db.ProjectGroups.Add(group);
                }
                sr.Close();
                file = null;
                System.IO.File.Delete(path);

                _db.SaveChanges();

                return RedirectToAction("CheckProjectGroup");
            }
            return RedirectToAction("Error", "Main", new {errorMessage = "Er is geen bestand geselecteerd"});
        }