【发布时间】:2015-02-11 06:25:07
【问题描述】:
我有这两个具有一对多关系的模型:
[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();
}
【问题讨论】:
标签: c# entity-framework