How to handle null value in Linq Select

filling error

The indicated order is invalid

using:

annual_leave_balance = (gaia == null) ? Convert.ToDouble("0") : gaia.Field<double?>("Annual Leave Balance"),

filling error

Object reference not set to object instance

if you use the following code to get the annual residual balance:

annual_leave_balance = (gaia["Annual Leave Balance"] == null) ? Convert.ToDouble("0") : gaia.Field<double>("Annual Leave Balance"),

var results = from bird in mssql_dataTable.AsEnumerable()
              join lion in dataTable.AsEnumerable() on bird.Field<Int32>("ExternalID") equals Convert.ToInt32(lion.Field<double>("External ID"))
              join gaia in Kiosk_mssql_dataTable.AsEnumerable() on bird.Field<Int32>("EmployeeID") equals Convert.ToInt32(gaia.Field<string>("StaffID"))
              into joinKioskEmp
              from gaia in joinKioskEmp.DefaultIfEmpty()
              select new
              {
                  employee_id = bird.Field<Int32>("EmployeeID"),
                  payrollnum = bird.Field<string>("PayrollNum"),
                  employee_name = (gaia != null) ? (gaia.Field<string>("First Name")+", "+gaia.Field<string>("Last Name")) : ((bird != null) ? (bird.Field<string>("FirstName")+", "+bird.Field<string>("Surname")) : ""),
                  //employee_name = (gaia != null) ? (gaia.Field<string>("First Name") + ", " + gaia.Field<string>("Last Name")) : "",
                  annual_leave_balance = (gaia["Annual Leave Balance"] == null) ? Convert.ToDouble("0") : gaia.Field<double>("Annual Leave Balance"),
                  _position = bird.Field<string>("position"),
                  external_id = bird.Field<Int32>("ExternalID"),
                  approver = lion.Field<string>("Approver"),
                  approver_email = lion.Field<string>("Approver Email Address")
              };
+3
source share
2 answers
annual_leave_balance = (gaia["Annual Leave Balance"] == null) 
      ? Convert.ToDouble("0") : gaia.Field<double>("Annual Leave Balance")

firstly you can replace Convert.ToDouble("0")with0d

secondly, you do not check for null, as in the previous line (gaia != null) ?

- This is problem?

+2
source

You can use Convert.DBNull

 annual_leave_balance = (gaia["Annual Leave Balance"] == Convert.DBNull) ? Convert.ToDouble("0") : gaia.Field<double>("Annual Leave Balance"),
+1
source

Source: https://habr.com/ru/post/1776660/


All Articles