I am trying to put a count inside select after a GroupJoin. You can find an example of what I am trying to do here. Basically I am trying to count how many times a player was used in home matches.
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()
});
Given the following data
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"},
};
I am expecting the output to be something like the following
{ 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 }
This works fine with list but not with actual database objects. I think it has to do with the Where.Count
but whatever I try I can't manage to get rid of the error(actually I can if I remove the Where.Count
clause completely).
Here is the error I am getting.
Unknown column 'Project6.id' in 'where clause' Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: MySql.Data.MySqlClient.MySqlException: Unknown column 'Project6.id' in 'where clause'
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
I am using EF6(6.1.3) and MySQL 5.7 (6.9.9).