在 c# 中搜索名称

问题描述 投票:0回答:1

我用 C# 编写了一段代码,用于在数据文件中搜索名称,作为每个名称下信息的参考。

这是我的数据文件:

John
09076809,45,3rd street, Sudanese,10.11.1979
Mathew 
05076899,37,3rd street, Japanese ,12.01.1987
Abrams 
01076033,27,3rd street, Sudanese,22.01.1997

这是我的代码:

public partial class MainForm : Form
    {
        private List<Person> peopleList;

        public MainForm()
        {
            InitializeComponent();
            InitializeData();
        }

        private void InitializeData()
        {
            // Read data from the text file and populate the peopleList
            peopleList = new List<Person>();

            try
            {
                string[] lines = File.ReadAllLines("people_data.txt");

                for (int i = 0; i < lines.Length; i += 6)
                {
                    string name = lines[i];
                    string[] info = lines[i + 1].Split(',');

                    string telephone = info[0].Trim();
                    int age = int.Parse(info[1].Trim());
                    string address = info[2].Trim();
                    string nationality = info[3].Trim();
                    DateTime birthday = DateTime.ParseExact(info[4].Trim(), "dd.MM.yyyy", null);

                    Person person = new Person(name, telephone, age, address, nationality, birthday);
                    peopleList.Add(person);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error reading data from file: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string searchName = textBox1.Text.Trim();

            if (!string.IsNullOrEmpty(searchName))
            {
                var searchResult = peopleList.Where(person => person.Name.Equals(searchName, StringComparison.OrdinalIgnoreCase)).ToList();

                if (searchResult.Any())
                {
                    dataGridView1.DataSource = searchResult;
                }
                else
                {
                    MessageBox.Show("No matching records found.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    dataGridView1.DataSource = null;
                }
            }
            else
            {
                MessageBox.Show("Please enter a name to search.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }

    public class Person
    {
        public string Name { get; set; }
        public string Telephone { get; set; }
        public int Age { get; set; }
        public string Address { get; set; }
        public string Nationality { get; set; }
        public DateTime Birthday { get; set; }

        public Person(string name, string telephone, int age, string address, string nationality, DateTime birthday)
        {
            Name = name;
            Telephone = telephone;
            Age = age;
            Address = address;
            Nationality = nationality;
            Birthday = birthday;
        }
    }

********"""""""" 当我运行代码时,我只收到了名字“John”。 让我看看我的错误在哪里。

arrays .net collections
1个回答
0
投票

问题可能出在您的“+= 6”语句上。这是因为每个人应该用两行表示(姓名、信息),而不是六行。

for (int i = 0; i < lines.Length; i += 2)
{
    string name = lines[i];
    string[] info = lines[i + 1].Split(',');

    string telephone = info[0].Trim();
    int age = int.Parse(info[1].Trim());
    string address = info[2].Trim();
    string nationality = info[3].Trim();
    DateTime birthday = DateTime.ParseExact(info[4].Trim(), "dd.MM.yyyy", null);

    Person person = new Person(name, telephone, age, address, nationality, birthday);
    peopleList.Add(person);
}
© www.soinside.com 2019 - 2024. All rights reserved.