views:

81

answers:

2
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?

+5  A: 

Do:

  var list = 
      (from i in _dataContext.aspnet_Users.Include("aspnet_Membership") 
        where string.IsNullOrEmpty(userName)
               || i.UserName.Contains(userName)  
       select i ).ToList();
Nix
A: 

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();
rossisdead
is he using linq to sql ?
Nix
Nix: You're right, I assumed too quickly. Fun fact still stands though ;)
rossisdead