![](/img/trans.png)
[英]System.NotSupportedException: 'The LINQ expression node type 'Invoke' is not supported in LINQ to Entities.'
[英]System.NotSupportedException: 'The specified type member 'StartDateTime' is not supported in LINQ to Entities
运行我的项目后,Visual Studio在我的Index.cshtml中向我展示了此System.NotSupportedException
这是我的HomeController
public class HomeController : BaseController
{
public ActionResult Index()
{
var events = this.db.Events
.OrderBy(e => e.StartDateTime)
.Where(e => e.IsPublic)
.Select(e => new EventViewModel()
{
Id = e.Id,
Title = e.Title,
Duration = e.Duration,
Author= e.Author.FullName,
Location = e.Location
});
var upcomingEvents = events.Where(e => e.StartDateTime > DateTime.Now);
var passedEvents = events.Where(e => e.StartDateTime <= DateTime.Now);
return View(new UpcomingPassedEventsViewModel()
{
UpcomingEvents = upcomingEvents,
PassedEvents = passedEvents
});
}
}
}
这是我的EventViewModel.cs
public class EventViewModel
{
public int Id { get; set; }
public string Title { get; set; }
public DateTime StartDateTime { get; set; }
public TimeSpan? Duration { get; set; }
public string Author { get; set; }
public string Location { get; set; }
}
您正在将实体投影到视图模型中。 这样做是一件好事,但是之后您将无法再次优化视图模型的查询。 您正在查询EventViewModel.StartDateTime
,甚至没有映射。
您需要在映射之前添加查询。 当然,您不想复制映射代码,因此将其放在方法中:
public ActionResult Index()
{
var events = this.db.Events
.OrderBy(e => e.StartDateTime)
.Where(e => e.IsPublic);
var upcomingEvents = events.Where(e => e.StartDateTime > DateTime.Now);
var passedEvents = events.Where(e => e.StartDateTime <= DateTime.Now);
return View(new UpcomingPassedEventsViewModel()
{
UpcomingEvents = upcomingEvents.Select(Map).ToList(),
PassedEvents = passedEvents.Select(Map).ToList()
});
}
private EventViewModel Map(EventDataModel e)
{
return new EventViewModel()
{
Id = e.Id,
Title = e.Title,
Duration = e.Duration,
Author= e.Author.FullName,
Location = e.Location
};
}
“ BugFinder”回答了我的问题。 我的事件没有保存StartDateTime。 所以,这就是答案
var events = this.db.Events
.OrderBy(e => e.StartDateTime)
.Where(e => e.IsPublic)
.Select(e => new EventViewModel()
{
Id = e.Id,
Title = e.Title,
StartDateTime = e.StartDateTime,
Duration = e.Duration,
Author= e.Author.FullName,
Location = e.Location
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.