private void toolStrip_Distribute_Click(object sender, EventArgs e) { if (DB_Queries.ExaminationHasMarks(_DB_Connection, SelectedExamID)) { MessageBox.Show("В экзамен уже включены абитуриенты. При повторном распределении они не будут удалены."); } Cursor.Current = Cursors.WaitCursor; var applications = _DB_Connection.Select( DB_Table.APPLICATIONS, new string[] { "id", "registration_time", "entrant_id" }, new List <Tuple <string, Relation, object> > { new Tuple <string, Relation, object>("passing_examinations", Relation.EQUAL, true), new Tuple <string, Relation, object>("status", Relation.EQUAL, "new") }).Where(a => (DateTime)a[1] >= (DateTime)dataGridView.SelectedRows[0].Cells[dataGridView_RegStartDate.Index].Value && (DateTime)a[1] < (DateTime)dataGridView.SelectedRows[0].Cells[dataGridView_RegEndDate.Index].Value + new TimeSpan(1, 0, 0, 0) ).Select(s => new { ApplID = (uint)s[0], EntrID = (uint)s[2] }); uint subjectID = _DB_Helper.GetDictionaryItemID(FIS_Dictionary.SUBJECTS, dataGridView.SelectedRows[0].Cells[dataGridView_Subject.Index].Value.ToString()); IEnumerable <uint> excludedAppls = DB_Queries.GetMarks( _DB_Connection, applications.Select(s => s.ApplID), Classes.Settings.CurrentCampaignID ).Where(s => s.FromExamDate.HasValue && (s.SubjID == subjectID || s.Value < _DB_Helper.GetMinMark(s.SubjID) )).Select(s => s.ApplID); applications = applications.Where(s => !excludedAppls.Contains(s.ApplID)); var applsDirections = _DB_Connection.Select( DB_Table.APPLICATIONS_ENTRANCES, "application_id", "faculty_short_name", "direction_id" ); var subjectDirections = _DB_Connection.Select( DB_Table.ENTRANCE_TESTS, new string[] { "direction_faculty", "direction_id" }, new List <Tuple <string, Relation, object> > { new Tuple <string, Relation, object>("campaign_id", Relation.EQUAL, Classes.Settings.CurrentCampaignID), new Tuple <string, Relation, object>("subject_id", Relation.EQUAL, subjectID) }); var applsSubjects = applsDirections.Join( subjectDirections, k1 => Tuple.Create(k1[1], k1[2]), k2 => Tuple.Create(k2[0], k2[1]), (s1, s2) => (uint)s1[0] ).Distinct(); applications = applications.Join( applsSubjects, k1 => k1.ApplID, k2 => k2, (s1, s2) => s1 ); if (applications.Count() == 0) { MessageBox.Show("Ни один абитуриент не попал в экзамен по условиям фильтрации.", "Предупреждение", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } var entrantsIDs = _DB_Connection.Select(DB_Table.ENTRANTS, "id").Join( applications, en => en[0], a => a.EntrID, (s1, s2) => s2.EntrID ).Distinct();//TODO Нужно? foreach (object entrID in entrantsIDs) { _DB_Connection.InsertOnDuplicateUpdate( DB_Table.ENTRANTS_EXAMINATIONS_MARKS, new Dictionary <string, object> { { "entrant_id", entrID }, { "examination_id", SelectedExamID } } ); } ToggleButtons(); Cursor.Current = Cursors.Default; }