我在关系数据库中有一个自定义实体,我已通过域模型映射到 CLR。因此,通过使用以下语句,我可以通过域模型上的 LINQ 查询将数据库中的实体拉入内存,如下所示;
var inspection = (from i in dbContext.New_testinspectionExtensionBases
where i.New_testinspectionId == currentInspection
select i).First();
我需要访问此实体上的属性/字段,我需要能够确定属性/字段名称及其值。我想遍历内存中的这些项目,并将它们的名称和值写到控制台。
我尝试使用这种方法,但无法弄清楚如何更正语法(我也不确定 GetProperties 是正确使用的方法,GetFields 出于某种原因没有返回任何内容,所以我认为这是要走的路)但这并不重要,因为我需要的只是对该值的读取权限;
var inspectionReportFields = inspection.GetType().GetProperties();
// I called this inspectionReportfields because the entity properties correspond to
// form/report fields I'm generating from this data.
foreach (var reportField in inspectionReportFields)
{
var value = reportField.GetValue();
Console.WriteLine(reportField.Name);
Console.WriteLine(value);
}
在使用 EF 或 openaccess 之类的域模型时,是否有更简单的方法来获取属性/字段值?如果没有,我会以正确的方式去做吗?最后,如果是这样,我该如何修复值变量声明中的语法?
以下是域模型生成的代码中的一些示例字段/属性,供参考;
private int? _new_systemGauges;
public virtual int? New_systemGauges
{
get
{
return this._new_systemGauges;
}
set
{
this._new_systemGauges = value;
}
}
private int? _new_systemAlarm ;
public virtual int? New_systemAlarm
{
get
{
return this._new_systemAlarm;
}
set
{
this._new_systemAlarm = value;
}
}