Twistar
Twistar

Reputation: 782

Linq to SQL searching

I have a table with users. I want to be able to search for a string and then return all username which contains this string. Then i want to populate a ListBox. This is what I have tried:

var varUser = (from u in dc.Users
                           where u.username == searchUserName
                           select u.username);
            lbSearchResult.DataSource = varUser;
            lbSearchResult.DataBind();

But when i try to search for "a" i don't get any results. It only works if i enter the full username.

Upvotes: 2

Views: 94

Answers (2)

Matten
Matten

Reputation: 17603

Maybe because of the clause

where u.username == searchUserName

Try u.username.Contains(searchUserName) or build a regular expression.

Upvotes: 1

Yuck
Yuck

Reputation: 50855

Try using Contains() instead:

var varUser = from u in dc.Users
              where u.username.Contains(searchUserName)
              select u.username;

Upvotes: 5

Related Questions