将日期时间转换为字符串实体框架
本文关键字:字符串 实体 框架 转换 日期 时间 | 更新日期: 2023-09-27 18:31:00
>我有以下代码,试图将d.dateofbirth和d.dateofdeath作为字符串返回,但没有太多运气。
我尝试使用 tostring() 或转换,但两者都在运行时转换为 sql,因此不起作用,
我需要能够在同一个网格视图单元格中的 2 行上显示 DOB 和 DOD,如下所示:
出生日期国防部
这是我到目前为止的代码:
fmsEntities context = new fmsEntities();
var query = from f in context.funerals
where f.IsPencil == 0
join d in context.deceaseddetails on f.DeceasedID equals d.ID
join i in context.funeralservices on f.ID equals i.FuneralID
where i.IsAlternative == 0
join h in context.htvalues on f.HtValuesID equals h.ID
join p in context.placeofdeaths on f.PlaceOfDeathID equals p.ID
join c in context.coroners on f.CoronerID equals c.ID
select new DataBindingProjection { DeceasedName = (d.LastName + Environment.NewLine + d.FirstName),
DOBDOD = Convert.ToString(d.DateOfBirth)};
var dataobjects = query.ToList();
dataGridView1.DataSource = dataobjects;
private class DataBindingProjection
{
public string DeceasedName {get; set;}
public string DOBDOD {get; set;}
}
更新,在构建数据投影之前移动到列表,如评论中的建议,
fmsEntities context = new fmsEntities();
var query = (from f in context.funerals
where f.IsPencil == 0
join d in context.deceaseddetails on f.DeceasedID equals d.ID
join i in context.funeralservices on f.ID equals i.FuneralID
where i.IsAlternative == 0
join h in context.htvalues on f.HtValuesID equals h.ID
join p in context.placeofdeaths on f.PlaceOfDeathID equals p.ID
join c in context.coroners on f.CoronerID equals c.ID
select new { f , d , i , h , p , c }).ToList();
var dataobjects = query.Select(d => new DataBindingProjection {DeceasedName = (d.LastName + Environment.NewLine + d.FirstName),
DOBDOD = (d.DateOfBirth.ToString() + Environment.NewLine + d.DateOfDeath.ToString())});
dataGridView1.DataSource = dataobjects;
}
private class DataBindingProjection
{
public string DeceasedName {get; set;}
public string DOBDOD {get; set;}
}
James,请考虑在第一次调用"ToList()"之后进行投影,如下所示:
var dataobjects = query
.ToList()
.Select(d => new DataBindingProjection { DeceasedName = (d.LastName + Environment.NewLine + d.FirstName),
DOBDOD = Convert.ToString(d.DateOfBirth)})
.ToList();