日期:2014-05-20  浏览次数:20888 次

linq中怎么表示sql中的字段 ='' or 字段 is null
本帖最后由 Smile_You 于 2012-12-10 23:58:37 编辑
linq中怎么表示sql中的字段  ='' or 字段 is null
还有(字段  !='' and not (字段  is null)) 这个呢?

我的写法....
                    if (MsgTel == 1)
                    {
                        cusList = cusList.Where(c => c.MsgTel.Contains("") || c.MsgTel == null).ToList();
                    }
                    if (MsgTel == 2)
                    {
                        cusList = cusList.Where(c => c.MsgTel != "" || c.MsgTel != null).ToList();
                    }


但是貌似没有任何效果...
并且    cusList = cusList.Where(c => c.MsgTel == "" || c.MsgTel == null).ToList(); 的时候还会报错
  我设定的  MsgTel  是string 类型的,sql类型为varchar(13) 可为空  
------解决方案--------------------
string.IsNullOrEmpty(c.MsgTel)
------解决方案--------------------
你是想问linq查询的时候怎么判断一个字段是否为空吧,
用==null,!=null就行了,手机党,凑合看
------解决方案--------------------
至少有5种方法来实现你的需求:
http://bbs.csdn.net/topics/380220328
------解决方案--------------------
cusList = cusList.Where(c => c.MsgTel != "" 
------解决方案--------------------
 c.MsgTel != null).ToList();
不管是""还是null都符合条件。。。
改成:
cusList = cusList.Where(c => c.MsgTel != "" && c.MsgTel != null).ToList();