在GroupJoin

时间:2016-12-18 17:38:55

标签: c# mysql entity-framework linq

我试图在GroupJoin之后将一个计数放入select中。您可以找到我正在尝试做的事情的示例here。基本上我试图计算一场球员在主场比赛中的使用次数。

var playerMatches = players
            .GroupJoin(matches, p => p.Id, m => m.PlayerId, (p, m) => new {player = p, matches = m})
            .Select(x => new {
                PlayerId = x.player.Id,
                PlayerName = x.player.Name,
                Home = x.matches.Where(m => m.Home.StartsWith("Y")).Count()
            });

鉴于以下数据

var players = new List<Player>{
    new Player{Id = 1, Name = "Player1"},
    new Player{Id = 2, Name = "Player2"},
    new Player{Id = 3, Name = "Player3"},
    new Player{Id = 4, Name = "Player4"},
    new Player{Id = 8, Name = "Player8"},
    new Player{Id = 11, Name = "Player11"}  
};

var matches = new List<Match>{
    new Match{Id = 10, Name = "Match10", PlayerId = 1, Home = "Y"},
    new Match{Id = 11, Name = "Match11", PlayerId = 1, Home = "Y"},
    new Match{Id = 12, Name = "Match12", PlayerId = 1, Home = "N"},
    new Match{Id = 13, Name = "Match13", PlayerId = 1, Home = "N"},
    new Match{Id = 14, Name = "Match14", PlayerId = 2, Home = "N"},
    new Match{Id = 15, Name = "Match15", PlayerId = 3, Home = "N"},
    new Match{Id = 16, Name = "Match16", PlayerId = 4, Home = "Y"},
    new Match{Id = 17, Name = "Match17", PlayerId = 4, Home = "N"},
    new Match{Id = 19, Name = "Match18", PlayerId = 11, Home = "Y"},
    new Match{Id = 18, Name = "Match19", PlayerId = 11, Home = "N"},
    new Match{Id = 20, Name = "Match20", PlayerId = 11, Home = "N"},
    new Match{Id = 21, Name = "Match21", PlayerId = 1, Home = "N"},
    new Match{Id = 22, Name = "Match22", PlayerId = 1, Home = "Y"},
    new Match{Id = 23, Name = "Match23", PlayerId = 8, Home = "N"},
    new Match{Id = 24, Name = "Match24", PlayerId = 1, Home = "Y"},
};

我希望输出类似于以下

{ PlayerId = 1, PlayerName = Player1, Home = 4 }
{ PlayerId = 2, PlayerName = Player2, Home = 0 }
{ PlayerId = 3, PlayerName = Player3, Home = 0 }
{ PlayerId = 4, PlayerName = Player4, Home = 1 }
{ PlayerId = 8, PlayerName = Player8, Home = 0 }
{ PlayerId = 11, PlayerName = Player11, Home = 1 }

这适用于list但不适用于实际的数据库对象。我认为它与Where.Count有关,但无论我尝试什么,我都无法摆脱错误(实际上我可以完全删除Where.Count子句。)

这是我得到的错误。

  

'where子句'中的未知列'Project6.id'   描述:执行期间发生未处理的异常   当前的网络请求。请查看堆栈跟踪了解更多信息   有关错误的信息以及它在代码中的起源。

     

异常详细信息:MySql.Data.MySqlClient.MySqlException:未知   'where子句'中的'Project6.id'列

     

来源错误:

     

执行期间生成了未处理的异常   当前的网络请求。有关的来源和位置的信息   可以使用下面的异常堆栈跟踪来识别异常。

我使用的是EF6(6.1.3)和MySQL 5.7(6.9.9)。

0 个答案:

没有答案