我在循环中为类对象赋值,显然我因日期空值而得到错误。
我的问题是我可以使用LINQ检查对象内的值
kubectl get pods
我需要检查三个日期变量是否为null。如果为null,那么我需要忽略它而不是将它们转换为日期
答案 0 :(得分:3)
如果要从查询中删除项目,可以添加
.Where(item => item.ProgressDate != null)
查询。
如果您不想解析null的日期但仍使用可以使用的项目
ProgressDate = item.ProgressDate != null ? Convert.ToDateTime(item.ProgressDate) :null
在对象创建中
答案 1 :(得分:1)
通过使用Convert.ToDateTime()
函数,我假设您的数据属于string
类型。
使用?:
运算符检查它是否为空。如果是,则使用默认的DateTime
值,否则转换。
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)
}));
此外,您可以将foreach
+ Add
替换为.Select
+ AddRange
答案 2 :(得分:1)
假设您的3个输入日期是DateTime的类型?如果为null,您想要默认(DateTime),您可以使用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()
});
}
答案 3 :(得分:0)
我将日期对话的字符串null检查为;
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)
});