如何使用 LINQ to CRM 填充实体只读字段(ModifiedOn、Createdby 等)?

Dar*_*ryl 1 c# linq dynamics-crm-2013

我正在尝试运行这个简单的查询:

var appt = (from a in context.AppointmentSet
           select new Appointment{ ModifiedOn = a.ModifiedOn}).First();
Run Code Online (Sandbox Code Playgroud)

但我收到编译器异常,因为 ModifiedOn 是只读的。

  • 我可以只返回a,但随后Appointment将返回实体的所有属性,而不仅仅是ModifiedOn.
  • 我可以返回new { a.ModifiedOn },但随后 appt 将是 anAnonymousType而不是Appointment.

使这项工作的建议方法是什么?

请注意,这是一个示例,假设我从 Appointment 返回的不仅仅是一个属性,然后还有某种 where 条件

Paw*_*cki 5

我总是这样做:

var appt = (from a in context.AppointmentSet
            select new Appointment { 
                Attributes = 
                { 
                    { "modifiedon", a.ModifiedOn },
                    { "createdon", a.CreatedOn },
                    { "createdby", a.CreatedBy }
                }
            })
            .First();
Run Code Online (Sandbox Code Playgroud)

它不需要任何额外的编码,我真的很惊讶没有人在这里发布它。