包含匹配参与者不起作用。当我调试时,它总是显示为Null。但是当我将GroupBy放入注释中时,它工作得很好。我使用的是代码优先的实体框架4.3.1。
实体:
public class Match
{
[ScaffoldColumn(false)]
public int MatchId { get; set; }
[Required(ErrorMessage = "Matchtype is a required field")]
public int Scheme { get; set; }
[Required]
[DefaultValue(false)]
public bool Finished { get; set; }
public int Round { get; set; }
// Relations
[Required]
public Category Category { get; set; }
public Official Official { get; set; }
public Slot Slot { get; set; }
public ICollection<MatchParticipant> MatchParticipants { get; set; }
}
public class MatchParticipant
{
[ScaffoldColumn(false)]
public int MatchParticipantId { get; set; }
public int Points { get; set; }
public int Goals { get; set; }
[Required]
public Match Match { get; set; }
[Required]
public Team Team { get; set; }
}
public class Team
{
[ScaffoldColumn(false)]
public int TeamId { get; set; }
[Required(ErrorMessage="Name is a required field")]
public string Name { get; set; }
[Required(ErrorMessage="Number of players is a required field")]
public int NumberOfPlayers { get; set; }
[Required(ErrorMessage="Coach is a required field")]
public string Coach { get; set; }
[Required(ErrorMessage="Phone is a required field")]
public string Phone { get; set; }
public string CellPhone { get; set; }
public string Fax { get; set; }
[Required(ErrorMessage="Email is a required field")]
public string Email { get; set; }
[Required(ErrorMessage="Address is a required field")]
public Address Address { get; set; }
public Pool Pool { get; set; }
[Required(ErrorMessage = "Category is a required field")]
public Category Category { get; set; }
public ICollection<MatchParticipant> matchParticipants { get; set; }
}
var matches =
context.matches
.Include("Official")
.Include("Slot")
.Include("MatchParticipants.Team")
.Include("Category.Tournament")
.Where(m => m.Category.Tournament.TournamentId == tournamentId)
.GroupBy(m => m.Category);
我怎样才能让Include工作呢?
发布于 2021-03-18 17:14:21
在GroupBy()
之后指定.Select()
中的includes。这将在结果中包含它们:
var result = ctx.SomeTable
.Where(t => t.IsWhateverTrue)
.GroupBy(t => t.MyGroupingKey)
.Select(g => new
{
Date = g.Key.Value,
Reservations = g.Select(m => new
{
m,
m.IncludedTable // this selects/includes the related table
})
}
);
https://stackoverflow.com/questions/10572328
复制相似问题