简体   繁体   中英

Check null condition for string date conversion to DateTime inside object

I am assigning values to class object in loop and apparently I am getting error due to date null value.

My question is can I use LINQ to check value inside object

foreach (var item in _query)
{
    EnrolmentList.Add(new EnrolmentEntity
    {
        PeopleUnitsID = item.PeopleUnitsID,
        PersonCode = item.PersonCode,
        UnitType = item.UnitType,
        ProgressCode = item.ProgressCode,
        ProgressStatus = item.ProgressStatus,
        ProgressDate = Convert.ToDateTime(item.ProgressDate),
        UnitInstanceID = item.UnitInstanceID,
        UnitInstanceOccurrenceID = item.UnitInstanceOccurrenceID,
        CourseCode = item.CourseCode,
        OwningOrganisation = item.OwningOrganisation,
        CalendarOccurrenceCode = item.CalendarOccurrenceCode,
        FES_Start_Date = Convert.ToDateTime(item.FES_Start_Date),
        AimStartDate = Convert.ToDateTime(item.AimStartDate)
    });
}

there are three date variable that I need to check if they are null or not. If null then I need to ignore that rather then to convert them to date

If you want to remove the items from the query you can add

.Where(item => item.ProgressDate != null)

to the query.

If you want to not parse the Dates that are null but still use the items you can use

ProgressDate = item.ProgressDate != null ? Convert.ToDateTime(item.ProgressDate) :null

inside your object creation

From the use of the Convert.ToDateTime() function I assume your data is of string type. Use the ?: operator check if it is null. If it is then use a default DateTime value, otherwise convert.

var defaultDate = DateTime.MinValue;
EnrolmentList.AddRange(_query.Select(item => new EnrolmentEntity
    {
        PeopleUnitsID = item.PeopleUnitsID,
        PersonCode = item.PersonCode,
        UnitType = item.UnitType,
        ProgressCode = item.ProgressCode,
        ProgressStatus = item.ProgressStatus,
        ProgressDate = item.ProgressDate == null ? defaultDate : Convert.ToDateTime(item.ProgressDate),
        UnitInstanceID = item.UnitInstanceID,
        UnitInstanceOccurrenceID = item.UnitInstanceOccurrenceID,
        CourseCode = item.CourseCode,
        OwningOrganisation = item.OwningOrganisation,
        CalendarOccurrenceCode = item.CalendarOccurrenceCode,
        FES_Start_Date = item.FES_Start_Date == null ? defaultDate : Convert.ToDateTime(item.FES_Start_Date),
        AimStartDate = item.AimStartDate == null ? defaultDate : Convert.ToDateTime(item.AimStartDate)
    }));

Also, you can replace the foreach + Add with a .Select + AddRange

Assuming your 3 input dates are typeof DateTime? AND you want default(DateTime) in case of null you can use GetValueOrDefault()

foreach (var item in _query)
        {
            EnrolmentList.Add(new EnrolmentEntity
            {
                PeopleUnitsID = item.PeopleUnitsID,
                PersonCode = item.PersonCode,
                UnitType = item.UnitType,
                ProgressCode = item.ProgressCode,
                ProgressStatus = item.ProgressStatus,
                ProgressDate = item.ProgressDate.GetValueOrDefault(),
                UnitInstanceID = item.UnitInstanceID,
                UnitInstanceOccurrenceID = item.UnitInstanceOccurrenceID,
                CourseCode = item.CourseCode,
                OwningOrganisation = item.OwningOrganisation,
                CalendarOccurrenceCode = item.CalendarOccurrenceCode,
                FES_Start_Date = item.FES_Start_Date.GetValueOrDefault(),
                AimStartDate = item.AimStartDate.GetValueOrDefault()
            });
        }

I have check string null for date conversation as;

  public class EnrolmentEntity
{

    public DateTime? ProgressDate { get; set; }

    public DateTime? FES_Start_Date { get; set; }

    public DateTime? AimStartDate { get; set; }
}

//

 public class Enrolments
 {    
    [XmlElement("PROGRESS_STATUS")]
    public string ProgressStatus { get; set; }

    [XmlElement("PROGRESS_DATE")]
    public string ProgressDate { get; set; }

    [XmlElement("FES_START_DATE")]
    public string FES_Start_Date  { get; set; }

    [XmlElement("AIM_START")]
    public string AimStartDate { get; set; } 

 }

//

foreach (var item in _query)
   {
     EnrolmentList.Add(new EnrolmentEntity
      {
        PeopleUnitsID = item.PeopleUnitsID,
        PersonCode = item.PersonCode,
        UnitType = item.UnitType,
        ProgressCode = item.ProgressCode,
        ProgressStatus = item.ProgressStatus,
        ProgressDate = string.IsNullOrEmpty(item.ProgressDate) ? (DateTime?)null : DateTime.Parse(item.ProgressDate), 
        UnitInstanceID = item.UnitInstanceID,
        UnitInstanceOccurrenceID = item.UnitInstanceOccurrenceID,
        CourseCode = item.CourseCode,
        OwningOrganisation = item.OwningOrganisation,
        CalendarOccurrenceCode = item.CalendarOccurrenceCode,
        FES_Start_Date = string.IsNullOrEmpty(item.FES_Start_Date) ? (DateTime?)null : DateTime.Parse(item.FES_Start_Date), 
        AimStartDate = string.IsNullOrEmpty(item.AimStartDate)? (DateTime?)null :DateTime.Parse(item.AimStartDate)  
 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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