C# 类型为';System.NotSupportedException';在ASP.Net程序中抛出

C# 类型为';System.NotSupportedException';在ASP.Net程序中抛出,c#,asp.net,asp.net-mvc,entity-framework,C#,Asp.net,Asp.net Mvc,Entity Framework,我想得到一份联赛比赛的名单,我得到了 EntityFramework.SqlServer.dll中发生“System.NotSupportedException”类型的异常,但未在用户代码中处理 我的视图代码如下所示: public ActionResult viewSchedule(int? id) { int leagueId = (int)id; int seasonYear = getSeasonYear(); League league = db.League

我想得到一份联赛比赛的名单,我得到了

EntityFramework.SqlServer.dll中发生“System.NotSupportedException”类型的异常,但未在用户代码中处理

我的视图代码如下所示:

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());         
}
视图模型为:

@模型IEnumerable

这行代码可能就是您得到错误的地方。您不能在EF查询内进行对象比较,因为EF需要将LINQ语句转换为SQL查询。您最好根据对象的ID来比较它们

db.Games.Where(l => l.League.Id == league.Id)
应该有用

这行代码可能就是您得到错误的地方。您不能在EF查询内进行对象比较,因为EF需要将LINQ语句转换为SQL查询。您最好根据对象的ID来比较它们

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

应该可以工作。

那么在哪一行抛出异常?
League
等。是自定义类还是一些库?尝试将b.Games.Where(l=>l.League==League)更改为b.Games.Where(l=>l.League.Id==leagueId),我不认为sql支持实体在查询中比较实体本身,此外,这还消除了首先搜索联盟实体的需要。当给定的.NET查询无法(由EF生成引擎)转换为本机SQL查询时,会引发此异常。搜索应该会产生许多重复/提示。
seasuredate==seasureyear
您是否将
DateTime
int
进行比较?此外,您应该能够将
.Where()
子句组合在一起。解决问题的Gusman如果您想将注释作为答案,我会将其标记为正确。那么在哪一行引发异常?
League
等。是自定义类还是一些库?尝试将b.Games.Where(l=>l.League==League)更改为b.Games.Where(l=>l.League.Id==leagueId),我不认为sql支持实体在查询中比较实体本身,此外,这还消除了首先搜索联盟实体的需要。当给定的.NET查询无法(由EF生成引擎)转换为本机SQL查询时,会引发此异常。搜索应该会产生许多重复/提示。
seasuredate==seasureyear
您是否将
DateTime
int
进行比较?另外,您应该能够将
.Where()
子句组合在一起。Gusman解决了这个问题如果您想让您的评论成为答案,我会将其标记为正确