-1

How can I select the Nullable Value from a column from the list.

Say for example I have a dataset converted into a list like below instead of passing a value in the client id(nullable column). I need to pass null. I've made the following attempts myself:

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"));

With all of the above methods, an InvalidCastException is thrown.

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459

1 Answers1

0

It's completely legal to compare nullable value with null:

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

Looks like you have DbNull.Value in either VisibleToAdmin or Required field. So, you should use nullable bool to get those fields values:

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");
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459