1
在Asp.net Web API我有以下模型。LINQ to Entities not supported
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。我怎樣才能格式化日期?
大,謝謝!如何獲取不同的日期部分在這裏描述:http://msdn.microsoft.com/de-de/library/ms174395.aspx。還有一種將月份作爲數字的方法嗎? – Explicat
@ user2558051對不起,忘了一個月左右。更新後的樣本將其作爲數字(您需要使用'StringConvert'和'DatePart') –