如何从lambda查询中删除DBnull值?

时间:2015-03-31 05:49:49

标签: c# asp.net asp.net-mvc linq-to-sql lambda

我有这个查询

        DataSetTest DB = new DataSetTest();
        DataSetTestTableAdapters.TESTTableAdapter ADPT = new DataSetTestTableAdapters.TESTTableAdapter();
        ADPT.Fill(DB.TEST);

         try
        {
            var xx = DB.TEST.Where(s => s.Name != null).Select(s => s.Name).ToList();
        }
        catch (Exception ex)
        {
            var x = ex;
        }

但它给了我这个错误

{"The value for column 'Name' in table 'TEST' is DBNull."}

所以我如何在列表中允许DBnull值或删除它多多提前感谢任何帮助

3 个答案:

答案 0 :(得分:0)

var xx = DB.TEST.Where(s => !string.IsNullOrEmpty(Convert.ToString(s.Name))).Select(s => s.Name).ToList();

答案 1 :(得分:0)

试试这个:

var xx = DB.TEST.Where(s => !string.IsNullOrEmpty(s.Name.ToString())).Select(s => s.Name).ToList();

s.NameDBNull时,ToString()将返回string.Empty

答案 2 :(得分:0)

谢谢大家,我只需要使用IsNameNull()方法

var xx = DB.TEST.Where(s => !s.IsNameNull()).Select(s => s.Name).ToList();