列表中对象内部的c#字段在linq之后被取消
IEnumerable<Game> games = await _context.Games
.Include(e => e.Challenger)
.Include(e => e.Opponent)
.Include(e => e.Winner)
.ToListAsync();
当我使用实体框架6 In .Net Core应用程序查询表时,我从数据库中获得了一个带有正确元素的列表,到目前为止,所有列表看起来都不错:
Charegerger
,ovsement
和winner
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)
在第二个元素Challenger
和Winner
中
被无效,并且有点随机进行。有些对象全部删除了。
试图“手动”以为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:
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; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,看看您的数据。在屏幕截图上,您可以看到下一步:ID = 3的项目具有您需要填写的所有字段,但是!它的完成字段等于“ true”值
然后在第二个屏幕截图上,您可以看到它被成品值等于“ false”

尝试运行查询
,您可能会看到不同的结果。希望这会有所帮助
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"

Try running the query like
And you will probably see different results. Hope this will help