Question: I have database with table tbl\_employee. In the table I store usernames. I use the following code to save all the usernames into a List:

```
string name = txtUsername.Text;       
List<string> lst = new List<string>();

NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;Port=5432;Database=db;User Id=postgres;Password=postgres;"); 

conn.Open();
string sql = "SELECT username FROM tbl_employee";
NpgsqlCommand command = new NpgsqlCommand(sql, conn);  
NpgsqlDataReader dr = command.ExecuteReader();  
while (dr.Read())
{
    lst.Add(dr.GetString(0));
}

```

Now, my question is how can I search through my list (lst) to see if the users input from textbox (name) exists in the list?

I have tried this:

```
if (lst.FindString(name)) 
    //Says it has some invalid arguments

```

And this:

```
if (lst.Exists(element => element == name)) 
    //It says name exists even though it doesn't

```

Answer: 
You could just use `Contains`:

```
if (lst.Contains(name)) ...

```

But if this is *all* you're doing with the list, I'd recommend changing this code so it queries the `tbl_empoyee` table directly from the database. I'm not familiar with the `NpgsqlCommand` but it should look a bit like this:

```
bool result = false;
string sql = "SELECT username FROM tbl_employee WHERE username = :name";
NpgsqlCommand command = new NpgsqlCommand(sql, conn);  
command.Parameters.AddWithValue("name", name);
NpgsqlDataReader dr = command.ExecuteReader();  
while (dr.Read())
{
    result = true; // record found
}

```

Or like this (following *Tim Schmelter*'s [suggestion](https://stackoverflow.com/questions/23415075/how-search-through-liststring-in-c/23415098#comment35880622_23415075)):

```
string sql = "SELECT COUNT(*) FROM tbl_employee WHERE username = :name";
NpgsqlCommand command = new NpgsqlCommand(sql, conn);  
command.Parameters.AddWithValue("name", name);
int found = (int)command.ExecuteScalar();  // 1 = found; 0 = not found

```
Try using `lst.Contains(name)`.