简体   繁体   中英

C# Fields inside objects in a list being nulled out after LINQ WHERE

 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:

So far so good img

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:

WTF img

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; }
}

Well, look at your data. On your screenshots, you can see next: an item with id = 3 has all the fields you need filled, but! it's Finished field is equal to "true" value

第一张截图,未过滤

Then on the second screenshot you can see that it's filtered by finished value equals to "false" 第二个屏幕截图,由finished == false过滤

Try running the query like

    games = games.Where(g => g.Finished == true); //or .Where(g => g.Finished)

And you will probably see different results. Hope this will help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM