bbrand0n
bbrand0n

Reputation: 31

How to search List<T> with user input string?

One of the features of my program is that it allows the user to enter a value to search through the list of contacts and have it display the contact info of the matched results. The search should find any contact where the field matches the target search string. I have tried both query and method syntax, and have read the chapter a million times but cant seem to get the hand of it. List:

            List<Contact> contacts = new List<Contact>();
        {
            contacts.Add(new Contact { firstName = "John", lastName = "Doe", phoneNumber = "7725551234", email = "[email protected]" });
            contacts.Add(new Contact { firstName = "Kent", lastName = "Woods", phoneNumber = "7725551445", email = "[email protected]" });
            contacts.Add(new Contact { firstName = "Jane", lastName = "Doe", phoneNumber = "7725553355", email = "[email protected]" });
            contacts.Add(new Contact { firstName = "Hank", lastName = "Fowland", phoneNumber = "7725558877", email = "[email protected]" });
            contacts.Add(new Contact { firstName = "Tracy", lastName = "Yates", phoneNumber = "7725552768", email = "[email protected]" });
            contacts.Add(new Contact { firstName = "Courtney", lastName = "Wall", phoneNumber = "7725556385", email = "[email protected]" });
            contacts.Add(new Contact { firstName = "Dawson", lastName = "Stokes", phoneNumber = "7725553098", email = "[email protected]" });
        };

And here is where I try to execute the search:

            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());
        }

I am so deep into trial and error that I've become lost. Thanks for the time!

Upvotes: 0

Views: 818

Answers (3)

Prasad Telkikar
Prasad Telkikar

Reputation: 16059

You can use Linq Where clause to find result as per users input

public static void Main()
{
    List<Contact> contacts = new List<Contact>();
    contacts.Add(new Contact { firstName = "John", lastName = "Doe", phoneNumber = "7725551234", email = "[email protected]" });
    contacts.Add(new Contact { firstName = "Kent", lastName = "Woods", phoneNumber = "7725551445", email = "[email protected]" });
    contacts.Add(new Contact { firstName = "Jane", lastName = "Doe", phoneNumber = "7725553355", email = "[email protected]" });
    contacts.Add(new Contact { firstName = "Hank", lastName = "Fowland", phoneNumber = "7725558877", email = "[email protected]" });
    contacts.Add(new Contact { firstName = "Tracy", lastName = "Yates", phoneNumber = "7725552768", email = "[email protected]" });
    contacts.Add(new Contact { firstName = "Courtney", lastName = "Wall", phoneNumber = "7725556385", email = "[email protected]" });
    contacts.Add(new Contact { firstName = "Dawson", lastName = "Stokes", phoneNumber = "7725553098", email = "[email protected]" });
    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;
    }
}

Here I considered user input can be any property value from Contact class

Upvotes: 0

lionthefox
lionthefox

Reputation: 439

What @Ashkan Mobayen Khiabani wrote, except for the typo in firs(t)Name :)

Also your should populate your list using this syntax to have cleaner code:

     var contacts = new List<Contact>
    {
        new Contact { firstName = "John", lastName = "Doe", phoneNumber = "7725551234", email = "[email protected]" },
        new Contact { firstName = "Kent", lastName = "Woods", phoneNumber = "7725551445", email = "[email protected]" },
        new Contact { firstName = "Jane", lastName = "Doe", phoneNumber = "7725553355", email = "[email protected]" }
    };

Upvotes: 0

Ashkan Mobayen Khiabani
Ashkan Mobayen Khiabani

Reputation: 34160

in your query contacts.ToString() would be a something like List<Contact>, you should just try to compare your input with each contact properties (ie. firstname, lastname,...):

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

Upvotes: 3

Related Questions