开发者

linq like empty string

开发者 https://www.devze.com 2022-12-27 16:25 出处:网络
var list = (from i in _dataContext.aspnet_Users.Include(\"aspnet_Members开发者_开发知识库hip\")where i.UserName.Contains(userName)select i ).ToList();
var list = (from i in _dataContext.aspnet_Users.Include("aspnet_Members开发者_开发知识库hip")  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?


Do:

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


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();
0

精彩评论

暂无评论...
验证码 换一张
取 消