如何使用用户输入的字符串搜索List <t>?

时间:2019-03-20 17:21:25

标签: c# list linq

该程序的功能之一是它允许用户输入一个值来搜索联系人列表,并使其显示匹配结果的联系人信息。搜索应找到该字段与目标搜索字符串匹配的任何联系人。我已经尝试过查询和方法语法,并且已经读了一百万遍了,但是似乎无法理解。 列表:

            List<Contact> contacts = new List<Contact>();
        {
            contacts.Add(new Contact { firstName = "John", lastName = "Doe", phoneNumber = "7725551234", email = "johndoe@email.com" });
            contacts.Add(new Contact { firstName = "Kent", lastName = "Woods", phoneNumber = "7725551445", email = "kentwoods@email.com" });
            contacts.Add(new Contact { firstName = "Jane", lastName = "Doe", phoneNumber = "7725553355", email = "janedoe@email.com" });
            contacts.Add(new Contact { firstName = "Hank", lastName = "Fowland", phoneNumber = "7725558877", email = "hankfowland@email.com" });
            contacts.Add(new Contact { firstName = "Tracy", lastName = "Yates", phoneNumber = "7725552768", email = "tracyyates@email.com" });
            contacts.Add(new Contact { firstName = "Courtney", lastName = "Wall", phoneNumber = "7725556385", email = "courtneywall@email.com" });
            contacts.Add(new Contact { firstName = "Dawson", lastName = "Stokes", phoneNumber = "7725553098", email = "dawsonstokes@email.com" });
        };

这是我尝试执行搜索的地方:

            else if (userChoice == 3)
        {
            Console.Write("\nWhat would you like to search for?: ");
            string search = Console.ReadLine();
            IEnumerable<string> results = from contact in contacts
                                         where contacts.ToString().Contains(search)
                                         select contact.ToString();
            var result = contacts.Find(x => x.Contains(search) );
            Console.WriteLine("{0}", results.ToList());
        }

我对试错法非常了解,以至于迷失了方向。感谢您的时间!

3 个答案:

答案 0 :(得分:3)

在您的查询contacts.ToString()中,类似List<Contact>,您应该尝试将输入内容与每个联系人属性(例如,姓,名,...)进行比较:

var result = contacts.Where(c => c.firsName.Contains(search) || c.lastName.Contains(search));

答案 1 :(得分:0)

您可以使用Linq Where子句按用户输入查找结果

public static void Main()
{
    List<Contact> contacts = new List<Contact>();
    contacts.Add(new Contact { firstName = "John", lastName = "Doe", phoneNumber = "7725551234", email = "johndoe@email.com" });
    contacts.Add(new Contact { firstName = "Kent", lastName = "Woods", phoneNumber = "7725551445", email = "kentwoods@email.com" });
    contacts.Add(new Contact { firstName = "Jane", lastName = "Doe", phoneNumber = "7725553355", email = "janedoe@email.com" });
    contacts.Add(new Contact { firstName = "Hank", lastName = "Fowland", phoneNumber = "7725558877", email = "hankfowland@email.com" });
    contacts.Add(new Contact { firstName = "Tracy", lastName = "Yates", phoneNumber = "7725552768", email = "tracyyates@email.com" });
    contacts.Add(new Contact { firstName = "Courtney", lastName = "Wall", phoneNumber = "7725556385", email = "courtneywall@email.com" });
    contacts.Add(new Contact { firstName = "Dawson", lastName = "Stokes", phoneNumber = "7725553098", email = "dawsonstokes@email.com" });
    string search = Console.ReadLine();
    var result = contacts.Where(c => c.firstName == search || c.lastName == search || c.phoneNumber == search || c.email == search).FirstOrDefault();


    Console.WriteLine(result.ToString()); // This will return all the values of Contact. Override ToString() function for you **Bonus
}

protected class Contact
{
    public string firstName { get; set; }
    public string lastName { get; set; }
    public string phoneNumber { get; set; }
    public string email { get; set; }

    public override string ToString()
    {
        return "FirstName =" + firstName + "\t LastName = " + lastName + "\t PhoneNumber = " + phoneNumber + "\t Email = " + email;
    }
}

在这里,我认为用户输入可以是Contact类中的任何属性值

答案 2 :(得分:0)

@Ashkan Mobayen Khiabani所写的内容,除了firs(t)Name中的错字:)

另外,您应该使用以下语法填充列表,以获取更简洁的代码:

     var contacts = new List<Contact>
    {
        new Contact { firstName = "John", lastName = "Doe", phoneNumber = "7725551234", email = "johndoe@email.com" },
        new Contact { firstName = "Kent", lastName = "Woods", phoneNumber = "7725551445", email = "kentwoods@email.com" },
        new Contact { firstName = "Jane", lastName = "Doe", phoneNumber = "7725553355", email = "janedoe@email.com" }
    };