/// <summary> /// Extention method for a DataContext object that checks if the object exists in the DataContext /// </summary> /// <param name="user"></param> /// <returns>True or false</returns> public static bool IsValid <T>(this System.Data.Linq.Table <T> table, T item) where T : class, System.ComponentModel.INotifyPropertyChanged { try { if (null == table) { return(false); } if (null == item) { return(false); } else { return(table.Where(i => i == item).FirstOrDefault() != null); } } catch { return(false); } }
private void buttonSearch_Click(object sender, EventArgs e) { this.Cursor = Cursors.WaitCursor; // выборка всех записей var res = tabDanie.Where <danie>(wh => wh.ID > 0); // отфильтровывание по номеру if (checkBoxNumber.Checked) { if (!cbNumberTo.Checked) { string[] mas = textBoxNumber.Text.Split(", ".ToCharArray()).Where(wh => !string.IsNullOrEmpty(wh) && !wh.Equals(",") && !wh.Equals(" ")).ToArray(); if (mas != null && mas.Length > 0) { int[] num = new int[mas.Length]; for (int i = 0; i < mas.Length; i++) { Int32.TryParse(mas[i], out num[i]); } res = res.Where <danie>(wh => num.Contains((int)wh.nomer)); } } else { int numFrom, numTo; if (Int32.TryParse(textBoxNumberFrom.Text, out numFrom) && Int32.TryParse(textBoxNumberTo.Text, out numTo)) { res = res.Where <danie>(wh => wh.nomer >= numFrom && wh.nomer <= numTo); } else { MessageBox.Show("Неверно введен один из номеров!"); } } } //выборка по основной и вторичным Рубрикам if (checkBoxTema.Checked) { if (!String.IsNullOrEmpty(textBoxTema.Text)) { res = res.Where <danie>(wh => wh.tema1.Contains(textBoxTema.Text) || wh.tema2.Contains(textBoxTema.Text)); } } // выборка по автору if (checkBoxAutor.Checked) { if (!String.IsNullOrEmpty(textBoxAutor.Text)) { res = res.Where <danie>(wh => wh.Aftor.Contains(textBoxAutor.Text)); } } // выборка по наименованию if (checkBoxName.Checked) { if (!String.IsNullOrEmpty(textBoxName.Text)) { res = res.Where <danie>(wh => wh.Naz.Contains(textBoxName.Text)); } } // выборка по организации if (checkBoxOrg.Checked) { if (!String.IsNullOrEmpty(textBoxOrg.Text)) { res = res.Where <danie>(wh => wh.Org.Contains(textBoxOrg.Text)); } } // выборка по городу if (checkBoxSity.Checked) { if (!String.IsNullOrEmpty(textBoxSity.Text)) { res = res.Where <danie>(wh => wh.Gorod.Contains(textBoxSity.Text)); } } // выборка по году if (checkBoxYear.Checked) { if (radioButtonFixYear.Checked) { int year; if (Int32.TryParse(textBoxYear1.Text, out year)) { res = res.Where <danie>(wh => wh.God == year); } else { MessageBox.Show("Неверно введен Год. " + "\"" + textBoxYear1.Text + "\""); } } if (radioButtonMoreYear.Checked) { int year1, year2; if (Int32.TryParse(textBoxYear1.Text, out year1) && Int32.TryParse(textBoxYear2.Text, out year2)) { res = res.Where <danie>(wh => wh.God >= year1 && wh.God <= year2); } else { MessageBox.Show("Проверьте правильность вводимых значений годов"); } } } // выборка по ключевым словам // выполняет ExComboQuery /* * if (checkBoxWords.Checked) * { * if (!String.IsNullOrEmpty(textBoxKeyWords.Text)) * { * string[] keywords = textBoxKeyWords.Text.Split(','); * string query = String.Empty; * * if (rbAndKW.Checked) * { * GetQuery(keywords, QueryParam.And, out query, ""); * res = res.Where<danie>(query, keywords.ToList(), ""); * * } * else if (rbOrKW.Checked) * { * GetQuery(keywords, QueryParam.Or, out query, ""); * res = res.Where<danie>(query, keywords.ToList()); * } * else if (rbComboKW.Checked) * { * GetComboQuery(textBoxKeyWords.Text, out query, true); * res = res.Where<danie>(query, keywords.ToList()); * } * } * } */ // исключение к ключевым словам // выполняет ExComboQuery /*if (checkBoxExeptionWords.Checked) * { * //if (!string.IsNullOrEmpty(textBoxExeptionWords.Text)) * //{ * if (!String.IsNullOrEmpty(textBoxExeptionWords.Text)) * { * string[] keywords = textBoxExeptionWords.Text.Split(','); * string query = ""; * * if (rbAndExKW.Checked) * { * GetQuery(keywords, QueryParam.And, out query, "!"); * * res = res.Where<danie>(query, keywords.ToList()); * } * else if (rbOrExKW.Checked) * { * GetQuery(keywords, QueryParam.Or, out query, "!"); * res = res.Where<danie>(query, keywords.ToList()); * } * else if (rbComboExKW.Checked) * { * GetComboQuery(textBoxExeptionWords.Text, out query, false); * * res = res.Where<danie>(query, keywords.ToList()); * * } * //MessageBox.Show(res.ToString()); * //if (keywords.Length > 0) * //{ * // DelSpace(ref keywords[0]); * * // query += "!(Slova.Contains(\"" + keywords[0] + "\")"; * // for (int i = 1; i < keywords.Length; i++) * // { * // DelSpace(ref keywords[i]); * // query += " || Slova.Contains(\"" + keywords[i] + "\")"; * // } * // query += ")"; * // res = res.Where<danie>(query, keywords.ToList()); * //} * } * //} * }*/ // выборка по реферату if (checkBoxReferat.Checked) { if (!String.IsNullOrEmpty(textBoxReferat.Text)) { res = res.Where <danie>(wh => wh.Referat.Contains(textBoxReferat.Text)); } } // выборка по месту хранения if (checkBoxPlace.Checked) { if (!String.IsNullOrEmpty(textBoxPlace.Text)) { res = res.Where <danie>(wh => wh.Mesto.Contains(textBoxPlace.Text)); } } if (cbSort.SelectedIndex == 0) { res = res.OrderBy(ob => ob.nomer); } else { res = res.OrderBy(ob => ob.God); } res.ToList(); //MessageBox.Show(res.ToString()); /*FileStream stream = new FileStream(@"query.txt", FileMode.OpenOrCreate, FileAccess.Write); * StreamWriter writer = new StreamWriter(stream); * writer.Write(res.ToString()); * writer.WriteLine(); * foreach (DbParameter par in dcont.GetCommand(res).Parameters) * writer.WriteLine(par.Value); * writer.Close(); */ try { // Сложный запрос /*if (rbComboKW.Checked && checkBoxWords.Checked) * * dGridView.DataSource = Util.ComboQueryResult(textBoxKeyWords.Text, String.Empty); */ if (checkBoxWords.Checked && checkBoxExeptionWords.Checked && checkBoxAutor.Checked) { if (rbAndKW.Checked && rbAndExKW.Checked) { dGridView.DataSource = Util.MultiQueryResult(textBoxKeyWords.Text, textBoxExeptionWords.Text, " AND ", " AND ", dcont.GetCommand(res), false, false, textBoxAutor.Text); } else if (rbAndKW.Checked && rbOrExKW.Checked) { dGridView.DataSource = Util.MultiQueryResult(textBoxKeyWords.Text, textBoxExeptionWords.Text, " AND ", " OR ", dcont.GetCommand(res), false, false, textBoxAutor.Text); } else if (rbAndKW.Checked && rbComboExKW.Checked) { dGridView.DataSource = Util.MultiQueryResult(textBoxKeyWords.Text, textBoxExeptionWords.Text, " AND ", String.Empty, dcont.GetCommand(res), false, true, textBoxAutor.Text); } else if (rbOrKW.Checked && rbAndExKW.Checked) { dGridView.DataSource = Util.MultiQueryResult(textBoxKeyWords.Text, textBoxExeptionWords.Text, " OR ", " AND ", dcont.GetCommand(res), false, false, textBoxAutor.Text); } else if (rbOrKW.Checked && rbOrExKW.Checked) { dGridView.DataSource = Util.MultiQueryResult(textBoxKeyWords.Text, textBoxExeptionWords.Text, " OR ", " OR ", dcont.GetCommand(res), false, false, textBoxAutor.Text); } else if (rbOrKW.Checked && rbComboExKW.Checked) { dGridView.DataSource = Util.MultiQueryResult(textBoxKeyWords.Text, textBoxExeptionWords.Text, " OR ", String.Empty, dcont.GetCommand(res), false, true, textBoxAutor.Text); } else if (rbComboKW.Checked && rbAndExKW.Checked) { dGridView.DataSource = Util.MultiQueryResult(textBoxKeyWords.Text, textBoxExeptionWords.Text, String.Empty, " AND ", dcont.GetCommand(res), true, false, textBoxAutor.Text); } else if (rbComboKW.Checked && rbOrExKW.Checked) { dGridView.DataSource = Util.MultiQueryResult(textBoxKeyWords.Text, textBoxExeptionWords.Text, String.Empty, " OR ", dcont.GetCommand(res), true, false, textBoxAutor.Text); } else if (rbComboKW.Checked && rbComboExKW.Checked) { dGridView.DataSource = Util.MultiQueryResult(textBoxKeyWords.Text, textBoxExeptionWords.Text, String.Empty, String.Empty, dcont.GetCommand(res), true, true, textBoxAutor.Text); } } else if (checkBoxWords.Checked && checkBoxExeptionWords.Checked) { if (rbAndKW.Checked && rbAndExKW.Checked) { dGridView.DataSource = Util.MultiQueryResult(textBoxKeyWords.Text, textBoxExeptionWords.Text, " AND ", " AND ", dcont.GetCommand(res), false, false, string.Empty); } else if (rbAndKW.Checked && rbOrExKW.Checked) { dGridView.DataSource = Util.MultiQueryResult(textBoxKeyWords.Text, textBoxExeptionWords.Text, " AND ", " OR ", dcont.GetCommand(res), false, false, string.Empty); } else if (rbAndKW.Checked && rbComboExKW.Checked) { dGridView.DataSource = Util.MultiQueryResult(textBoxKeyWords.Text, textBoxExeptionWords.Text, " AND ", String.Empty, dcont.GetCommand(res), false, true, string.Empty); } else if (rbOrKW.Checked && rbAndExKW.Checked) { dGridView.DataSource = Util.MultiQueryResult(textBoxKeyWords.Text, textBoxExeptionWords.Text, " OR ", " AND ", dcont.GetCommand(res), false, false, string.Empty); } else if (rbOrKW.Checked && rbOrExKW.Checked) { dGridView.DataSource = Util.MultiQueryResult(textBoxKeyWords.Text, textBoxExeptionWords.Text, " OR ", " OR ", dcont.GetCommand(res), false, false, string.Empty); } else if (rbOrKW.Checked && rbComboExKW.Checked) { dGridView.DataSource = Util.MultiQueryResult(textBoxKeyWords.Text, textBoxExeptionWords.Text, " OR ", String.Empty, dcont.GetCommand(res), false, true, string.Empty); } else if (rbComboKW.Checked && rbAndExKW.Checked) { dGridView.DataSource = Util.MultiQueryResult(textBoxKeyWords.Text, textBoxExeptionWords.Text, String.Empty, " AND ", dcont.GetCommand(res), true, false, string.Empty); } else if (rbComboKW.Checked && rbOrExKW.Checked) { dGridView.DataSource = Util.MultiQueryResult(textBoxKeyWords.Text, textBoxExeptionWords.Text, String.Empty, " OR ", dcont.GetCommand(res), true, false, string.Empty); } else if (rbComboKW.Checked && rbComboExKW.Checked) { dGridView.DataSource = Util.MultiQueryResult(textBoxKeyWords.Text, textBoxExeptionWords.Text, String.Empty, String.Empty, dcont.GetCommand(res), true, true, string.Empty); } } else if (checkBoxWords.Checked && checkBoxAutor.Checked) { if (rbComboKW.Checked) { dGridView.DataSource = Util.ComboQueryResult(textBoxKeyWords.Text, String.Empty, dcont.GetCommand(res), true, false, textBoxAutor.Text); } else if (rbAndKW.Checked) { dGridView.DataSource = Util.ComboQueryResult(textBoxKeyWords.Text, " AND ", dcont.GetCommand(res), false, false, textBoxAutor.Text); } else if (rbOrKW.Checked) { dGridView.DataSource = Util.ComboQueryResult(textBoxKeyWords.Text, " OR ", dcont.GetCommand(res), false, false, textBoxAutor.Text); } } else if (checkBoxExeptionWords.Checked && checkBoxAutor.Checked) { if (rbComboExKW.Checked) { dGridView.DataSource = Util.ComboQueryResult(textBoxExeptionWords.Text, String.Empty, dcont.GetCommand(res), true, true, textBoxAutor.Text); } else if (rbAndExKW.Checked) { dGridView.DataSource = Util.ComboQueryResult(textBoxExeptionWords.Text, " AND ", dcont.GetCommand(res), false, true, textBoxAutor.Text); } else if (rbOrExKW.Checked) { dGridView.DataSource = Util.ComboQueryResult(textBoxExeptionWords.Text, " OR ", dcont.GetCommand(res), false, true, textBoxAutor.Text); } } else if (checkBoxWords.Checked) { if (rbComboKW.Checked) { dGridView.DataSource = Util.ComboQueryResult(textBoxKeyWords.Text, String.Empty, dcont.GetCommand(res), true, false, string.Empty); } else if (rbAndKW.Checked) { dGridView.DataSource = Util.ComboQueryResult(textBoxKeyWords.Text, " AND ", dcont.GetCommand(res), false, false, string.Empty); } else if (rbOrKW.Checked) { dGridView.DataSource = Util.ComboQueryResult(textBoxKeyWords.Text, " OR ", dcont.GetCommand(res), false, false, string.Empty); } } else if (checkBoxExeptionWords.Checked) { if (rbComboExKW.Checked) { dGridView.DataSource = Util.ComboQueryResult(textBoxExeptionWords.Text, String.Empty, dcont.GetCommand(res), true, true, string.Empty); } else if (rbAndExKW.Checked) { dGridView.DataSource = Util.ComboQueryResult(textBoxExeptionWords.Text, " AND ", dcont.GetCommand(res), false, true, string.Empty); } else if (rbOrExKW.Checked) { dGridView.DataSource = Util.ComboQueryResult(textBoxExeptionWords.Text, " OR ", dcont.GetCommand(res), false, true, string.Empty); } } else if (checkBoxAutor.Checked) { if (!String.IsNullOrEmpty(textBoxAutor.Text)) { dGridView.DataSource = Util.AuthorQueryResult(textBoxAutor.Text, dcont.GetCommand(res)); } } else if (!checkBoxWords.Checked && !checkBoxExeptionWords.Checked && !checkBoxAutor.Checked) { dGridView.DataSource = res; } } catch { MessageBox.Show("Некорректно введены параметры запроса"); } finally { this.Cursor = Cursors.Default; label1.Text = dGridView.Rows.Count.ToString() + " записей"; } }