0

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; }
}
P.Belberov
  • 503
  • 1
  • 5
  • 18
  • 1
    Your event only seems to hold id, title, duration, author and location - no startdatetime – BugFinder Sep 11 '17 at 10:45
  • 1
    You have to provide the code for your `EventViewModel` class. It is very likely that the `StartDateTime` property is not mapped to a DB column. But to check that we need the code. – Sefe Sep 11 '17 at 10:47
  • 1
    Possible duplicate of [How do I perform Date Comparison in EF query?](https://stackoverflow.com/questions/1088212/how-do-i-perform-date-comparison-in-ef-query) – Deblaton Jean-Philippe Sep 11 '17 at 10:48
  • I added my EventViewModel – P.Belberov Sep 11 '17 at 10:52

2 Answers2

0

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
    };
}
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
0

"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
            });
P.Belberov
  • 503
  • 1
  • 5
  • 18