如何使用 lambda 表达式访问正确的数据类型?
Posted
技术标签:
【中文标题】如何使用 lambda 表达式访问正确的数据类型?【英文标题】:How to use lambda expression to access correct data type? 【发布时间】:2021-12-21 23:10:50 【问题描述】:我正在使用 lambda 表达式来访问具有数据类型的值,但问题是我的本地数据库上的 Time 数据类型为 Time(7) 并使用实体框架。在我的模型中,此数据类型定义为 DateTime
。
我现在如何访问这种数据类型?
这是我的代码:
public List GetIncident_Details()
Entities incident = new Entities();
List result = new List();
var c_incident = incident.Incident_Template.Select(c => c).ToList();
if (c_incident != null && c_incident.Count() > 0)
foreach (var cData in c_incident)
Incident_DropDown model = new Incident_DropDown();
model.Title = cData.Title;
model.Description = cData.Description;
model.Date_Occurred = cData.Date_Occurred;
// How do I change this to have access?
// It's complaining about the data type object being set to a string?
model.Time = cData.Time;
model.Assignment_Group = cData.Assignment_Group;
model.Reported_CI = cData.Reported_CI;
result.Add(model);
return result;
public class Incident_DropDown
public string Title get; set;
public string Description get; set;
public string Date_Occurred get; set;
public DateTime Time get; set; // Time
public string Assignment_Group get; set;
public string Reported_CI get; set;
【问题讨论】:
如果DateTime
,您是否尝试使用TimeSpan
?
@AlexyRumnyantsev 没有
【参考方案1】:
从@alexey-rumyantsev 那里得到了一些建议,然后不得不通过询问模型数据类型的时间来测试我的代码,它是日期时间,然后更改为时间跨度。在测试此数据类型时,与我的本地数据库记录进行比较,并且在调试时传递了正确的值。 // 模型名称
public class Incident_DropDown
public string Title get; set;
public string Description get; set;
public string Date_Occured get; set;
public TimeSpan Time get; set; // had to change to work
public string Assignment_Group get; set;
public string Reported_CI get; set;
// Controller
public List<Incident_DropDown> GetIncident_Details()
Entities incident = new Entities();
List<Incident_DropDown> result = new List<Incident_DropDown>();
var c_incident = incident.Incident_Template.Select(c => c).ToList();
if (c_incident != null && c_incident.Count() > 0)
foreach (var cData in c_incident)
Incident_DropDown model = new Incident_DropDown();
model.Title = cData.Title;
model.Description = cData.Description;
model.Date_Occured = cData.Date_Occured;
model.Time = cData.Time; // This here enable to pass correct time as per database record
model.Assignment_Group = cData.Assignment_Group;
model.Reported_CI = cData.Reported_CI;
result.Add(model);
return result;
【讨论】:
以上是关于如何使用 lambda 表达式访问正确的数据类型?的主要内容,如果未能解决你的问题,请参考以下文章