An exception of type 'System.NotSupportedException' thrown in ASP.Net program

asp.net asp.net-mvc c# entity-framework

Question

Im trying to get a list of league games and am getting

An exception of type 'System.NotSupportedException'occurred in EntityFramework.SqlServer.dll but was not handled in user code

My code for the view is as follows:

public ActionResult viewSchedule(int? id)
{
    int leagueId = (int)id;
    int seasonYear = getSeasonYear();

    League league = db.Leagues.Find(leagueId);
    var leagueGames = db.Games.Where(l => l.League == league).Where(g => g.SeasonDate == seasonYear);

    return View(leagueGames.ToList());         
}

and the view model is:

@model IEnumerable

1
0
4/5/2016 7:08:24 PM

Accepted Answer

db.Games.Where(l => l.League == league)

This line of code is where you are probably getting the error. You cannot do object comparisons inside EF query because EF needs to convert your LINQ statements to SQL queries. You should better compare objects by their ids.

db.Games.Where(l => l.League.Id == league.Id)

should work.

4
4/5/2016 6:40:06 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow