-1

如何从列表中的列中选择 Nullable 值。

例如,我将一个数据集转换为如下所示的列表,而不是在客户端 ID(可空列)中传递一个值。我需要传递空值。我自己做了以下尝试:

var reqlist = (from list in tableList.AsEnumerable()
                where (list.Field<int?>("ClientID") == clientID)
                       && list.Field<bool>("VisibleToAdmin") == true
                       && list.Field<bool>("Required") == true
                select list.Field<string>("FieldName"));

1.

var reqlist = (from list in tableList.AsEnumerable()
                 where (list.Field<int?>("ClientID") == null)
                        && list.Field<bool>("VisibleToAdmin") == true
                        && list.Field<bool>("Required") == true
                 select list.Field<string>("FieldName"));

2.

var reqlist = (from list in tableList.AsEnumerable()
                 where (list.Field<int?>("ClientID") == (int?)(null))
                        && list.Field<bool>("VisibleToAdmin") == true
                        && list.Field<bool>("Required") == true
                 select list.Field<string>("FieldName"));

3.

var reqlist = (from list in tableList.AsEnumerable()
                 where (list.Field<int?>("ClientID") == (bool?)(null))
                        && list.Field<bool>("VisibleToAdmin") == true
                        && list.Field<bool>("Required") == true
                 select list.Field<string>("FieldName"));

4.

var reqlist = (from list in tableList.AsEnumerable()
                 where (list.IsNull("ClientID"))
                        && list.Field<bool>("VisibleToAdmin") == true
                        && list.Field<bool>("Required") == true
                 select list.Field<string>("FieldName"));

使用上述所有方法,InvalidCastException都会抛出 an 。

4

1 回答 1

0

将可空值与 null 进行比较是完全合法的:

list.Field<int?>("ClientID") == null
// same as 
!list.Field<int?>("ClientID").HasValue

看起来您DbNull.ValueVisibleToAdmin必填字段中都有。因此,您应该使用可为空的 bool 来获取这些字段值:

int? clientID = null;
var reqlist = from r in tableList.AsEnumerable()
              where r.Field<int?>("ClientID") == clientID &&
                    r.Field<bool?>("VisibleToAdmin") == true &&
                    r.Field<bool?>("Required") == true
              select r.Field<string>("FieldName");
于 2013-07-01T07:55:31.977 回答