小编bde*_*ino的帖子

迭代通过域模型上的linq查询返回的对象的属性和值

我在关系数据库中有一个自定义实体,我已经通过域模型映射到CLR.因此,通过使用以下语句,我可以通过域模型上的LINQ查询将实体从我的数据库中提取到内存中,就像这样;

var inspection = (from i in dbContext.New_testinspectionExtensionBases
              where i.New_testinspectionId == currentInspection   
              select i).First();                         
Run Code Online (Sandbox Code Playgroud)

我需要访问此实体上的属性/字段,我需要能够确定属性/字段名称及其值.我想在内存中循环这些项,并将其名称和值写出到控制台.

我尝试使用这种方法,但无法弄清楚如何纠正语法(我也不确定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);
}
Run Code Online (Sandbox Code Playgroud)

在使用像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 …
Run Code Online (Sandbox Code Playgroud)

c# linq entity-framework telerik-open-access

39
推荐指数
1
解决办法
8万
查看次数

标签 统计

c# ×1

entity-framework ×1

linq ×1

telerik-open-access ×1