我正在尝试使用Linq To Sql并在尝试在我的Web应用程序中执行以下查询时遇到问题:
var query = from t in teamsTable
where t.SeasonTeams.All(x => x.SeasonID != 4)
select t;
return query.ToList();
此时,我收到以下错误:
会员访问'Int32 SeasonID' 'DomainModel.Entities.SeasonTeam'没有 合法的类型 “System.Collections.Generic.IList`1 [DomainModel.Entities.SeasonTeam]。
使用LINQPad,完全相同的查询工作。我在下面列出了Linq Mappings的类。我非常感谢帮助解决问题并解释导致问题的原因。
[Table(Name = "Teams")]
public class Team
{
[Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
public int TeamID { get; set; }
[Column]
public string Name { get; set; }
[Association(OtherKey = "TeamID")]
private EntitySet<Player> _players = new EntitySet<Player>();
public IList<Player> TeamPlayers { get { return _players.ToList(); } }
[Association(OtherKey = "HomeTeamID")]
private EntitySet<Fixture> _homeFixtures = new EntitySet<Fixture>();
public IList<Fixture> HomeFixtures { get { return _homeFixtures.ToList(); } }
[Association(OtherKey = "AwayTeamID")]
private EntitySet<Fixture> _awayFixtures = new EntitySet<Fixture>();
public IList<Fixture> AwayFixtures { get { return _awayFixtures.ToList(); } }
[Association(OtherKey="TeamID")]
private EntitySet<SeasonTeam> _seasonteams = new EntitySet<SeasonTeam>();
public IList<SeasonTeam> SeasonTeams { get { return _seasonteams.ToList(); } }
}
[Table(Name = "Seasons")]
public class Season
{
[Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
public int SeasonID { get; set; }
[Column]
public DateTime StartDate { get; set; }
[Column]
public DateTime EndDate { get; set; }
[Column]
public int LeagueID { get; set; }
[System.Data.Linq.Mapping.Association(OtherKey = "SeasonID")]
private EntitySet<Fixture> _fixtures = new EntitySet<Fixture>();
public IList<Fixture> Fixtures { get { return _fixtures.ToList().AsReadOnly(); } }
[System.Data.Linq.Mapping.Association(OtherKey = "SeasonID")]
private EntitySet<SeasonTeam> _seasonteams = new EntitySet<SeasonTeam>();
public IList<SeasonTeam> SeasonTeams { get { return _seasonteams.ToList().AsReadOnly(); } }
internal EntityRef<League> _league;
[System.Data.Linq.Mapping.Association(ThisKey = "LeagueID", Storage = "_league")]
public League League
{
get { return _league.Entity; }
internal set { _league.Entity = value; LeagueID = value.LeagueID; }
}
}
[Table(Name="SeasonTeams")]
public class SeasonTeam
{
[Column(IsDbGenerated = true, IsPrimaryKey = true, AutoSync = AutoSync.OnInsert)]
public int SeasonTeamID { get; set; }
[Column]
public int TeamID { get; set; }
[Column]
public int SeasonID { get; set; }
internal EntityRef<Team> _team;
[Association(ThisKey = "TeamID", Storage = "_team")]
public Team Team
{
get { return _team.Entity; }
set { _team.Entity = value; TeamID = value.TeamID; }
}
internal EntityRef<Season> _season;
[Association(ThisKey = "SeasonID", Storage = "_season")]
public Season Season
{
get { return _season.Entity; }
set { _season.Entity = value; SeasonID = value.SeasonID; }
}
}