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

How to remove DBnull value from lambda query?

我有这个查询

        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 值或将其删除非常感谢任何帮助

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

试试这个:

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

s.NameDBNull时,ToString()将return string.Empty

谢谢大家,我已经用 IsNameNull() 方法解决了

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