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
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.