列表中对象内部的c#字段在linq之后被取消

发布于 2025-02-10 01:31:42 字数 2223 浏览 1 评论 0原文

 IEnumerable<Game> games = await _context.Games
            .Include(e => e.Challenger)
            .Include(e => e.Opponent)
            .Include(e => e.Winner)
            .ToListAsync();

当我使用实体框架6 In .Net Core应用程序查询表时,我从数据库中获得了一个带有正确元素的列表,到目前为止,所有列表看起来都不错:

到目前为止,

Charegergerovsementwinner winner 都有一个带有玩家对象每个的正确信息。

然后,我尝试使用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);
        }

这是我不理解的奇怪的东西,列表中的某些字段被删除:

​ 被无效(及其ID)

在第二个元素ChallengerWinner

被无效,并且有点随机进行。有些对象全部删除了。

试图“手动”以为Linq是一个问题:

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

但是结果相同,这使我发疯。我是否错过了我没有看到的明显的东西?

实体:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白云悠悠 2025-02-17 01:31:42

好吧,看看您的数据。在屏幕截图上,您可以看到下一步:ID = 3的项目具有您需要填写的所有字段,但是!它的完成字段等于“ true”值

”第一个屏幕截图,未过滤”

然后在第二个屏幕截图上,您可以看到它被成品值等于“ false”

尝试运行查询

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

,您可能会看到不同的结果。希望这会有所帮助

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

first screenshot, not filtered

Then on the second screenshot you can see that it's filtered by finished value equals to "false"
second screenshot, filtered by 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文