我知道可能已经问过这个标题,但是这些解决方案建议使用ExpandoObject
这对我没用。
我读过:
我正在使用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;
}
}