使用 Entity Framework 将日期作为字符串传递给 Crystal 报告

Pass date as String to Crystal Report Using Entity Framework

当 DateOfBirth 为 Null 时,我想将类似“-”的字符串传递给 Crystal 报告,但出现此错误:

System.FormatException: 'String was not recognized as a valid DateTime.'

使用此代码时:

 .Select(u => new
{

 u.EmployeeCode,
 u.EmployeeName,
 JopName = string.IsNullOrEmpty(u.JopName) ? "-" : u.JopName,
 Date_Hiring = u.Date_Hiring.GetValueOrDefault(),
 AdministrationName = string.IsNullOrEmpty(u.AdministrationName) ? "-" : u.AdministrationName,
 DepartmentName = string.IsNullOrEmpty(u.DepartmentName) ? "-" : u.DepartmentName,
 PranchName = string.IsNullOrEmpty(u.PranchName) ? "-" : u.PranchName,
 DateOfBirth =Convert.ToString(u.DateOfBirth) == string.Empty ? DateTime.Parse("-") : u.DateOfBirth
})
 .ToList();

这是我的 Class :

public partial class SR1_Result
 {
   public int EmployeeCode { get; set; }
   public string EmployeeName { get; set; }
   public string JopName { get; set; }
   public Nullable<System.DateTime> Date_Hiring { get; set; }
   public Nullable<double> Nat_Salary { get; set; }
   public string AdministrationName { get; set; }
   public string DepartmentName { get; set; }
   public string PranchName { get; set; }
   public string MobilePersonalNo { get; set; }
   public string MobileWorkNo { get; set; }
   public Nullable<System.DateTime> DateOfBirth { get; set; }
}

没测试过,但不会是这样吧:

DateOfBirth = u.DateOfBirth == null ? "-" : u.DateOfBirth.Value.ToString("<your date format here>")