我有這兩款機型與一對多的關係:「方法不能被翻譯成店表達」怪異的行爲
[Table("User")]
public class User
{
public User()
{
Times = new HashSet<Time>();
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public Guid Guid { get; set; }
public virtual ICollection<Time> Times { get; set; }
}
[Table("Time")]
public class Time
{
[Key]
public long TimeId { get; set; }
public DateTime WorkDay { get; set; }
public Guid UserGuid { get; set; }
public virtual User User { get; set; }
}
和方法在上下文類返回的DataTable。 先執行失敗的查詢經過.ToDataTable()擴展(或.ToList()或其他) 有異常後:
LINQ to Entities does not recognize the method 'System.String ToShortDateString()' method, and this method cannot be translated into a store expression
第二個順利完美的罰款。 問題是爲什麼?
第一次執行。它不起作用
public DataTable GetDtProjectsForUser(User user)
{
var query = from time in Time
select new
{
WorkDay = time.WorkDay.ToShortDateString(),
};
return query.ToDataTable();
}
第二個。它工作
public DataTable GetDtProjectsForUser(User user)
{
var localUser = User.Find(user.Guid);
var query = from time in localUser.Times
select new
{
WorkDay = time.WorkDay.ToShortDateString(),
};
return query.ToDataTable();
}
是的,我知道延期執行。但在這兩種情況下,「查詢」在我調用ToDataTable擴展時執行(至少我是這麼認爲的)。 – Szer 2015-02-11 06:40:31
@Szer,是的。這是推遲的意思。這兩個查詢在您調用ToDataTable方法時執行。但是linq執行不像正常執行。爲了更好地理解linq,也更喜歡[LINQ如何在內部工作](http://stackoverflow.com/questions/671235/how-linq-works-internally) – 2015-02-11 06:43:45