IEnumerable<Game> games = await _context.Games
.Include(e => e.Challenger)
.Include(e => e.Opponent)
.Include(e => e.Winner)
.ToListAsync();
When i query a table using Entity Framework 6 in .Net Core App, i get a list with the correct elements from the database, all looks good so far:
Challenger, Opponent and Winner all has a Player object with the correct info for each.
Then i try to filter the data using LINQ:
IEnumerable<Game> games = await _context.Games
.Include(e => e.Challenger)
.Include(e => e.Opponent)
.Include(e => e.Winner)
.ToListAsync();
if (finished.HasValue)
{
games = games.Where(g => g.Finished == finished.Value);
}
This is where weird stuff happens that i dont understand, some fields inside the objects in the list becomes nulled out:
As shown in the image, in the first element, Opponent and Winner are nulled out (and their ids)
In the second element Challenger and Winner are nulled out
And it kinda goes on randomly like that. Some objects has all three nulled out.
Tried to filter "manually" thinking LINQ was the problem:
if (finished.HasValue)
{
//games = games.Where(g => g.Finished == finished.Value);
List<Game> newList = new List<Game>();
foreach (var game in games)
{
if (game.Finished == finished.Value)
{
newList.Add(game);
}
}
}
But same result and it is driving me crazy. Am i missing something obvious that I'm not seeing?
Entities:
public class Game
{
public int? Id { get; set; }
public int? ChallengerId { get; set; }
public Player? Challenger { get; set; }
public int? OpponentId { get; set; }
public Player? Opponent { get; set; }
public bool? Finished { get; set; }
public int? WinnerId { get; set; }
public Player? Winner { get; set; }
public DateTime? GameDate { get; set; }
public int? EloShift { get; set; }
}
public class Player
{
public int Id { get; set; }
public string? PlayerName { get; set; }
public int? Wins { get; set; }
public int? Losses { get; set; }
public int? Rating { get; set; }
}

