简体   繁体   English

如何在Linq Select中处理空值

[英]How to treat null value in Linq Select

fill error 填写错误

Specified cast is not valid 指定演员表无效

if using : 如果使用:

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

fill error 填写错误

Object reference not set to an instance of an object 你调用的对象是空的

if using the following code to get annual leave 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")
              };
annual_leave_balance = (gaia["Annual Leave Balance"] == null) 
      ? Convert.ToDouble("0") : gaia.Field<double>("Annual Leave Balance")

firstly, you can replace Convert.ToDouble("0") to 0d 首先,您可以将Convert.ToDouble("0")替换为0d

secondly you are not checking for null as in your previous line (gaia != null) ? 其次,你没有像前一行那样检查null (gaia != null) ?

is this the problem ? 这是问题吗?

您可以使用Convert.DBNull

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM