这是我的问题:我有一个类有 2 个相同类类型的列表属性(但对如何填充有一些不同的限制),比方说:
public class Team
{
[Key]
public int IDTeam { get; set; }
public string TeamName { get; set; }
public List<Programmer> Members { get; set; }
public List<Programmer> Leaders { get; set; }
public LoadLists(MyProjectDBContext db)
{
this.Members = db.Programmers.Where(p => p.IDTeam = this.IDTeam
&& (p.Experience == "" || p.Experience == null)).ToList();
this.Leaders = db.Programmers.Where(p => p.IDTeam = this.IDTeam
&& (p.Experience != null && p.Experience != "")).ToList();
}
}
public class Programmer
{
[Key]
public int IDProgrammer { get; set; }
[ForeignKey("Team")]
public int IDTeam { get; set; }
public virtual Team Team { get; set; }
public string Name { get; set; }
public string Experience { get; set; }
}
在某些时候,我需要列出团队的成员和领导者,为此我会假设:
return db.Teams
.Include(m => m.Members.Where(p => p.Experience == "" || p.Experience == null)
.Include(l => l.Leaders.Where(p => p.Experience != null && p.Experience != "")
.OrderBy(t => t.TeamName)
.ToList();
而且,当然,在这种情况下,我会假设它是错误的(因为它根本不起作用)。关于如何实现这一目标的任何想法?
编辑:为了澄清一点,团队类的 2 个列表属性应根据以下内容填写:
1 - 成员属性 - 应包括所有没有经验的相关程序员(proggramer.Experience == null 或“”);
2 - 领导者属性 - 应包括所有具有任何经验的相关程序员(programmer.Experiente != null 或“”);
编辑 2:这是 MyProjectDbContext 声明:
public class MyProjectDBContext : DbContext
{
public DbSet<Team> Teams { get; set; }
public DbSet<Programmer> Programmers { get; set; }
}