void importStudentSelectionsCommand() { var cl = base.GetClCase(base.LocalID); var preselections = (from c in cl.Courses from cc in c.Levels select new UIPreselection() { CourseID = c.ID, Course = c.Name, LevelID = cc.ID, Level = cc.Name, })?.ToList(); ImportStudentSelectionWindow window = new ImportStudentSelectionWindow(); window.Closed += (s, args) => { if (window.DialogResult.Value) { var tables = window.DT; var columnsCount = tables.Columns.Count; foreach (var st in this.Students) { st.Preselections?.Clear(); } this.Students.Clear(); this.ShowLoading = true; ObservableCollection <UIStudent> students = new ObservableCollection <UIStudent>(); Task.Run(() => { for (int i = 1; i < tables.Rows.Count; i++) { var name = tables.Rows[i][0].ToString(); if (string.IsNullOrEmpty(name)) { continue; } var student = students.FirstOrDefault(st => st.Name.Equals(name)); if (student == null) { int number = students.Count == 0 ? 0 : students.Max(st => Convert.ToInt32(st.ID)); int no = students.Count == 0 ? 0 : students.Max(st => st.NO); student = new UIStudent() { ID = (number + 1).ToString(), NO = no + 1, Name = name, }; students.Add(student); } // 遍历其它志愿 for (int h = 1; h < columnsCount; h++) { var value = tables.Rows[i][h].ToString(); if (!string.IsNullOrEmpty(value)) { var columnName = tables.Rows[0][h].ToString(); // 如果有层没有输入层的名字,直接输入科目的名字。 var firstCourse = cl.Courses.FirstOrDefault(clc => clc.Name.Equals(value)); if (firstCourse?.Levels?.Count > 1) { continue; } UIPreselection selection; var selections = preselections.Where(p => p.Course.Equals(columnName))?.ToList(); if (!columnName.Equals(value)) { selection = selections?.FirstOrDefault(ss => ss.Level.Equals(value)); } else { selection = selections.FirstOrDefault(); } if (selection != null) { var has = student.Preselections.Any(p => p.Course.Equals(selection.Course)); if (!has) { student.Preselections.Add(selection); } } } } } }).ContinueWith((r) => { this.Students = students; GalaSoft.MvvmLight.Threading.DispatcherHelper.CheckBeginInvokeOnUI(() => { this.loadDataGrid(); this.RefreshStudentFromUI(); _dg.ItemsSource = this.Students; _studentCollectionView = (ListCollectionView)CollectionViewSource.GetDefaultView(this.Students); _studentCollectionView.Filter = StudentContains; }); this.ShowLoading = false; }); } }; window.ShowDialog(); }
void importStudentSelectionsCommand() { var cl = CommonDataManager.GetCLCase(base.LocalID); var preselections = (from c in cl.Courses from cc in c.Levels select new UIPreselection() { CourseID = c.ID, Course = c.Name, LevelID = cc.ID, Level = cc.Name, })?.ToList(); ImportStudentSelectionWindow window = new ImportStudentSelectionWindow(); window.Closed += (s, args) => { if (window.DialogResult.Value) { // 清空当前志愿 //foreach (var st in this.Students) //{ // st.Preselections?.Clear(); //} //清空所有学生 this.Students.Clear(); var tables = window.DT; var columnsCount = tables.Columns.Count; // 内容 for (int i = 1; i < tables.Rows.Count; i++) { var name = tables.Rows[i][0].ToString(); if (string.IsNullOrEmpty(name)) { continue; } var student = this.Students.FirstOrDefault(st => st.Name.Equals(name)); if (student == null) { int number = this.Students.Count == 0 ? 0 : this.Students.Max(st => Convert.ToInt32(st.ID)); int no = this.Students.Count == 0 ? 0 : this.Students.Max(st => st.NO); student = new UIStudent() { ID = (number + 1).ToString(), NO = no + 1, Name = name, }; this.Students.Add(student); } // 遍历其它志愿 for (int h = 1; h < columnsCount; h++) { var value = tables.Rows[i][h].ToString(); if (!string.IsNullOrEmpty(value)) { var columnName = tables.Rows[0][h].ToString(); // 如果有层没有输入层的名字,直接输入科目的名字。 var firstCourse = cl.Courses.FirstOrDefault(clc => clc.Name.Equals(value)); if (firstCourse?.Levels?.Count > 1) { continue; } UIPreselection selection; var selections = preselections.Where(p => p.Course.Equals(columnName))?.ToList(); if (!columnName.Equals(value)) { selection = selections?.FirstOrDefault(ss => ss.Level.Equals(value)); } else { selection = selections.FirstOrDefault(); } if (selection != null) { var has = student.Preselections.Any(p => p.Course.Equals(selection.Course)); if (!has) { student.Preselections.Add(selection); } } } } } // 重新绑定界面 windowCommand(_view); } }; window.ShowDialog(); }