带有关联表的c# Join子句

本文关键字:Join 子句 关联 | 更新日期: 2023-09-27 18:14:36

我有三个班:(人事、外科和手术)

    public class Personnel
{
    [Key]
    public int CodePersonel { get; set; }
    public FullName NomComplet { get; set; }
    public Adresse Adress { get; set; }
    public int Age { get; set; }
    public ICollection<Operation> Operation { get; set; }
}

Chirurgien

public class Chirurgien : Personnel
{
    public int Nbre_anne_Exp { get; set; }
    public int NoteXP { get; set; }
}

和操作:

    public class Operation
{
    public int OperationId { get; set; }
    public DateTime DateDebut { get; set; }
    public DateTime DateFin { get; set; }
    public int Duree { get; set; }
    public bool Etat { get; set; }
    public string CIN { get; set; }
    public ICollection<Personnel> Personel { get; set; }
    public Patient Patients { get; set; }
    public override string ToString()
    {
        return CIN;
    }
}

我还创建了一个关联表member

HasMany(p => p.Personel).WithMany(v => v.Operation).Map(m => {
            m.ToTable("Membre");
                m.MapLeftKey("Operation");
                m.MapRightKey("Personel");
            });

我怎么能得到手术失败的手术列表(操作Etat=false) ??

我使用以下代码返回完整的Chirurgien列表:

public ICollection<Chirurgien> NoobDoctors()
    {
        var req = from t in ut.getRepository<Chirurgien>().GetAll()
                  select t;

        return req.ToList();
    }

谢谢

带有关联表的c# Join子句

我怎么能得到手术失败的手术列表(操作Etat=false) ??

您可以使用Operation导航属性与Any过滤:

var result = from c in ut.getRepository<Chirurgien>().GetAll()
             where c.Operation.Any(o => !o.Etat)
             select c;

由于您已经将many-to-many关系配置为隐式连接表,EF将为您维护表(包括查询连接)