【问题标题】:System.NotSupportedException: 'The specified type member 'StartDateTime' is not supported in LINQ to EntitiesSystem.NotSupportedException:“LINQ to Entities 不支持指定的类型成员“StartDateTime”
【发布时间】:2018-02-19 14:00:44
【问题描述】:

运行我的项目后,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; }
}

【问题讨论】:

  • 您的活动似乎只包含 id、标题、持续时间、作者和位置 - 没有开始日期时间
  • 您必须为您的EventViewModel 课程提供代码。 StartDateTime 属性很可能未映射到数据库列。但是要检查我们是否需要代码。
  • 我添加了我的 EventViewModel

标签: c# visual-studio linq notsupportedexception


【解决方案1】:

“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
            });

【讨论】:

    【解决方案2】:

    您将实体投影到视图模型中。这是一件好事,但之后您无法再次优化您的视图模型的查询。您正在查询 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
        };
    }
    

    【讨论】:

      猜你喜欢
      • 2015-01-27
      • 2012-07-17
      • 2013-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-28
      • 1970-01-01
      • 2015-07-25
      相关资源
      最近更新 更多