2

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.

2 Answers 2

5

Try using Contains() instead:

var varUser = from u in dc.Users
              where u.username.Contains(searchUserName)
              select u.username;
Sign up to request clarification or add additional context in comments.

2 Comments

It's worth noting that it can be case ignorant with u.user.ToLower().Contains(searchUserName.ToLower())
@ThePower Contains within LINQ-SQL will generate a LIKE statement that is case insensitive by default in MSSQL unless otherwise specified.
1

Maybe because of the clause

where u.username == searchUserName

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.