3
var list = (from i in _dataContext.aspnet_Users.Include("aspnet_Membership")  where i.UserName.Contains(userName)  select i ).ToList();

if userName="" then nothing return. how can i do that if empty string then return all records?

1
  • Please clarify, I don't have a clue what you're asking. - Edit - on second thought, I think I've got it. Commented Apr 19, 2010 at 21:15

2 Answers 2

7

Do:

  var list = 
      (from i in _dataContext.aspnet_Users.Include("aspnet_Membership") 
        where string.IsNullOrEmpty(userName)
               || i.UserName.Contains(userName)  
       select i ).ToList();
Sign up to request clarification or add additional context in comments.

Comments

2

Fun Fact: The System.Data.Linq.SqlClient namespace includes a few helper methods that are pretty useful.

You can use the SqlMethods.Like function which will return all results if an empty string is passed to it.

Ex:

 (from i in _dataContext.aspnet_Users.Include("aspnet_Membership") 
  where SqlMethods.Like(i.UserName, "%" + userName + "%")
  select i).ToList();

1 Comment

Nix: You're right, I assumed too quickly. Fun fact still stands though ;)

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.