user4912134
user4912134

Reputation: 1043

Check a string exists in the query response c#

I am getting a response from the select query and storing them in to the string list

        string abc = "123";
        List<string> numb = new List<string>();
        string selectQuery = "SELECT Number FROM NumberList;";
        using (MySqlConnection conn = new MySqlConnection(connStr))
        using (MySqlCommand cmd = new MySqlCommand(selectQuery, conn))
        {
            conn.Open();
            var reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                numb.Add(reader.GetString(0));
            }

I am not sure how to check the string abc exists in numb. I dont want to use for loop for this because the count of records returned from query response is high. Is there any easy of checking if the 123 exists in the list or not.

Upvotes: 0

Views: 214

Answers (2)

Nino
Nino

Reputation: 7115

while (reader.Read())
{
    var s = reader.GetString(0);
    if (!numb.Contains(s))
    {
        numb.Add(s);
    }
}

Upvotes: 0

BackDoorNoBaby
BackDoorNoBaby

Reputation: 1455

This?

if (numb.Contains(abc)) 
{
   // List contains the string contained in variable abc
}

Upvotes: 2

Related Questions