简体   繁体   中英

System.NotSupportedException: 'The specified type member 'StartDateTime' is not supported in LINQ to Entities

After running my project visual studio has shown me this System.NotSupportedException in my Index.cshtml

在此处输入图片说明

Here's my 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
        });
    }
}

}

Here is my 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; }
}

You're projecting your entities into a view model. That's a good thing to do, but afterwards you can't refine the query for your viewmodel again. You're querying on EventViewModel.StartDateTime , which you don't even map.

You need to add the query before mapping. Of course you don't want to duplicate the mapping code, so put it in a method:

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" answered my question. My events doesn't hold StartDateTime. So, here is the answer

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM