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

134 views Asked by At

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

enter image description here

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

There are 2 answers

0
P.Belberov On

"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
            });
0
CodeCaster On

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