我有这两款机型与一对多的关系:“方法不能被翻译成店表达”怪异的行为
[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