在 Asp.net Web API 中,我有以下模型。
public class User
{
[Required, Key]
public long Id { get; set; }
public string Name { get; set; }
public Company Employer { get; set; }
[Required]
public DateTime DateAdded { get; set; }
}
这是其中一个控制器的摘录。
// GET api/Company/5
public Object GetCompany(int id)
{
var query = from user in db.Users
join company in db.Companies on user.Employer.Id equals company.Id
where company.Id == id
orderby user.Name
select new { Id = user.Id, Name = user.Name, EmailAddress = user.EmailAddress, Version = user.DateAdded.ToString("dd.MM.yyyy"), Employer = company.Name };
return query.AsEnumerable<Object>();
}
我想要的是格式化 DateAdded 属性。但是,当我尝试调用用户属性之一的方法或调用用户的方法时,EF 会引发 NotSupportedException。我怎样才能格式化日期?