我无法将ISO
DateTime
格式值仅转换为日期值。我尝试了Parse
,ParseExact
和TryParseExact
并且都失败了,并继续给我相同的值输出(1984-04-26T00:00:00
)。
我提到了许多references和其他SO问题但没有帮助
我正在使用DTO,这是属性,
public DateTime Dob { get; set; }
我正在运行for
循环来加入LINQ
次查询中的数据,我正在尝试按以下方式进行转换,
for (int i = 0; i < PatientInfo.Count; i++)
{
PatientInfo[i].Dob =
DateTime.Parse(String.Format("{0:MM/dd/yyyy}", PatientInfo[i].Dob.ToString()));
PatientInfo[i].PartnerData = PartnerInfo.Where(a => a.FileId == PartnerInfo[i].FileId).ToList();
}
我在这里缺少什么?
以下是API controller
代码,
[HttpGet("{id}")]
public async Task<IActionResult> GetPatReg([FromRoute] long id)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var PatientInfo = await _context.PatReg
.Where(a => a.FileId == id)
.Select(b => new PatRegDto
{
Action = "Get",
FileId = b.FileId,
FName = b.FName,
MName = b.MName,
LName = b.LName,
Dob = b.Dob
}).ToListAsync();
var PartnerInfo = await _context.PatPar
.Where(s => s.FileId == id)
.Select(m => new PatParDto
{
RecId = m.RecId,
FileId = m.FileId,
ParFileId = m.ParFileId,
SDate = m.SDate,
EDate = m.EDate,
}).ToListAsync();
for (int i = 0; i < PartnerInfo.Count; i++)
{
PartnerInfo[i].FullName = _context.PatReg.Where(a => a.FileId == PartnerInfo[i].ParFileId)
.Select(t => new { t.fullname })
.Single().fullname;
PartnerInfo[i].dob = _context.PatReg.Where(a => a.FileId == PartnerInfo[i].ParFileId)
.Select(t => new { t.Dob })
.Single().Dob;
PartnerInfo[i].Action = "Get";
}
for (int i = 0; i < PatientInfo.Count; i++)
{
PatientInfo[i].Dob =
DateTime.Parse(String.Format("{0:MM/dd/yyyy}", PatientInfo[i].Dob.ToString()));
PatientInfo[i].PartnerData = PartnerInfo.Where(a => a.FileId == PartnerInfo[i].FileId).ToList();
}
if (PatientInfo == null)
{
return NotFound();
}
var DataRes = new
{
sdata = PatientInfo
};
return Ok(DataRes);
}
更新 我的DTO是
public class PatRegDto
{
public string Action { get; set; }
private Int64 _FileId;
public Int64 FileId
{
get
{
return this._FileId;
}
set
{
this._FileId = value;
}
}
public string FName { get; set; }
public string MName { get; set; }
public string LName { get; set; }
public string fullname
{
get { return FName + " " + MName + " " + LName; }
}
public DateTime Dob { get; set; }
public List<PatParDto> PartnerData { get; set; }
}
public class PatParDto
{
public string Action { get; set; }
public long RecId { get; set; }
public long FileId { get; set; }
public long ParFileId { get; set; }
public DateTime SDate { get; set; }
public DateTime? EDate { get; set; }
public DateTime dob { get; set; }
public string FullName { get; set; }
}
答案 0 :(得分:1)
请不要使用PatientInfo[i].Dob.ToString())
,因为它会使用您当前的区域性设置序列化为字符串。取决于您的操作系统语言设置,您将有不同的字符串。除了DateTime
包含返回日期的Date属性;)
答案 1 :(得分:0)
这是唯一的解决方案吗?
我向public string DobFormat { get; set; }
PatRegDto
添加DTO
,然后
for (int i = 0; i < PatientInfo.Count; i++)
{
PatientInfo[i].DobFormat = PatientInfo[i].Dob.ToString("MM/dd/yyyy");
PatientInfo[i].PartnerData = PartnerInfo.Where(a => a.FileId == PartnerInfo[i].FileId).ToList();
}
答案 2 :(得分:0)
上述所有有价值的评论的另一个答案,
公共课PatRegDto
{
public string Action { get; set; }
private Int64 _FileId;
public Int64 FileId
{
get
{
return this._FileId;
}
set
{
this._FileId = value;
}
}
public string FName { get; set; }
public string MName { get; set; }
public string LName { get; set; }
public string fullname
{
get { return FName + " " + MName + " " + LName; }
}
public string Dob { get; set; }
public List<PatParDto> PartnerData { get; set; }
}
并在控制器中
var PatientInfo = await _context.PatReg
.Where(a => a.FileId == id)
.Select(b => new PatRegDto
{
Action = "Get",
FileId = b.FileId,
FName = b.FName,
MName = b.MName,
LName = b.LName,
Dob = (b.Dob).ToString("dd/M/yyyy"),
}).ToListAsync();