In general, the question is similar to the topic title. How does having a list item know where legs grow from? For one thing you can be able to suggest options for implementing the search. I have several listings with data and textbox. Lists, for example, store names in this form (Last Name; First Name * Age $ date). In the textbox I enter the "Last Name" divided by Split by ";" symbol and memorize the index in the array. I need to compare the entered last name with the one in the array. If there is data there, then extract the remaining information about the person, knowing the name of the list and the index of the element, also divide by characters and add. Excuse me for apparently inventing a bicycle.
- Yes, apparently, a bicycle ... Let's start from the very beginning - if I understand correctly, there are certain data sets, there is user input. It is necessary to check whether there is something that the user entered in the data set, and, if the input is there, then output additional data from the data set. So? - BlackWitcher
- Yes that's right. After the separation by the symbol I lose the add. data. - igrize
- Then I would alter the approach to working with data in general, if possible. How exactly - a lot of ways, from tables in the database to create a special class for this. I will try to give an answer by the way with the creation of the class. - BlackWitcher
3 answers
To store your data, let's create the simplest class whose fields would be Last Name, First Name, Middle Name, Age, Date, etc ... Let this class be called Person . After that, we are already creating a new list, the elements of which will be instances of this class, and now we will work with them.
//Класс для хранения данных public class Person { public string _family; //Фамилия public string _name; //Имя public string _surname; //Отчество public int _age; //Возраст public DateTime _birthdate; //Дата рождения //Конструктор класса (может быть не обязательно таким) public Person(string family, string name, string surname, string age, DateTime birthdate) { this._family = family; this._name = name; this._surname = surname; this._age = 40; this._birthdate = birthdate; } } ... //Теперь объявим коллекцию для хранения данных. List<Person> people = new List<Person>(); //Добавляем новую запись Person human = new Person("Иванов", "Иван", "Иванович", 40, DateTime.Parse("11.10.1976")); people.Add(human); ... And then you can work with people both through LINQ, and directly, for example, like this:
foreach(var p in people) { if (p.Name == "Иван") { MessageBox.Show("Полные данные: " + p._family + " " + p._name + " " + p._surname + " Возраст: " + p._age.ToString() + " Дата рождения: " + p._birthdate.ToShortDateString()); } } Accordingly, the code is given as an example only.
- Thank you very much! The answer is more than exhaustive! =))) PS Having picked up my code a little, I still achieved adequate work. True to extract all extras. data took 6 cycles ... I'm afraid to imagine how he would work with large lists xD Thank you! I ’ll better use your option =)) - Igrize
- @Igrize, you're welcome! - BlackWitcher
The short answer to the question: No.
In your case, the first thing you need to do when you receive a new line (or array of strings) of the form (Last Name; First Name * Age $ date) is to initialize with this line a new instance of the Person class (written by you, the class contains the fields last name, first name, age, date , search methods and overloaded comparison methods).
In the Sheet, you store objects of class Person. When you receive a request to search by last name / first name / age / date - filter the list using the LINQ (List.Where (..)) query and then work with the result as you need.
PS: When parsing a string (Last Name; First Name * Age $ Date) I recommend using regular expressions. To simplify their compilation, I would recommend Resharper's designer.
Let you have a class (the answer was given above) Person , and you have a list of people. List<Person> People When typing in a textbox (if you are not friends with MVVM), you can handle, say, the KeyDown event and register in the handler
MyListBox.SelectedItem = People.Where(p => p.Famaly.ToLower().Contains(MyTextBox.Text.ToLower()));