如何从动态对象中检索字段

时间:2017-09-29 15:49:57

标签: c# entity-framework

我知道可能已经问过这个标题,但是这些解决方案建议使用ExpandoObject这对我没用。

我读过:

  1. access properties from anonymous types
  2. dynamic does not contain a definition for a property
  3. 我正在使用Entity Framework 6,我有一个匿名对象(取决于一些不同的表),我必须使用动态对象。

    如何从动态对象中检索字段?

    public void main()
    {
        dynamic caller = new ExpandoObject();
        caller = CustomerServices.GetByCallerId(callerId);
        DateTime CallDateTime = caller.CallDateTime; // Cause the Error
        int CallerId = caller.CallerId; // Cause the Error
    }
    
    public dynamic GetByCallerId(string callerId)
    {
        dynamic customerCall = new ExpandoObject();        
        using (MehrpouyanEntities dbContext = new MehrpouyanEntities())
        {
            customerCall = dbContext.Customers.Where(r => r.LandlinePhone1 == callerId || r.LandlinePhone2 == callerId || r.MobilePhone1 == callerId || r.MobilePhone2 == callerId).Select(r=>new {Caller = "myCaller", CallerId = r.Id, Name = r.Name, PhoneNumber = callerId, Address = r.Address, CallDateTime = DateTime.Now}).FirstOrDefault();
            return customerCall;
        }
    }
    

0 个答案:

没有答案