使用 where 子句的 Linq 多对多查询
Linq many to many query with where clause
我遇到以下问题:
我有一些表 "clinic"、"doctor"、"clinicDoctor"(代表多对多的关系),还有 "specialty" 和 "doctorSpecialty"(代表多对多的关系)关系)。
我想进行一个 linq 查询,以查找所有拥有特定专业医生的诊所。
在简历中,我想按专业 ID 查找诊所
var clinicLst = (from cli in clinica.Clinic
.Include(x => x.ClinicDoctor) //This is a Collection
.ThenInclude(x => x.IdDoctorNavigation)
.ThenInclude(x => x.DoctorSpecialty) //This is a Collection
.ThenInclude(x => x.IdSpecialtyNavigation)
.Where(cli => (cli.State == true)
&& HERE I WANT THE ESPECIALIDAD.ID == ID BUT I DON'T KNOW HOW)
select cli).ToList();
请多多指教。
提前致谢!!!
你的问题不是很清楚,但我认为你正在寻找类似的东西:
var clinicList = await clinica.Clinic.Where(x => x.DoctorSpecialty.Any(s => s.Id == specialtyId)).ToListAsync();
更新
因此 "your question isn't very clear"。通常,对于多对多,尤其是如果您使用的是 EF Core,您应该设置如下:
public class Doctor
{
...
public ICollection<DoctorSpecialty> Specialties { get; set; }
}
public class Specialty
{
...
public ICollection<DoctorSpecialty> Doctors { get; set; }
}
public class DoctorSpecialty
{
public int DoctorId { get; set; }
public int SpecialtyId { get; set; }
}
然后,Clinic
会是这样的:
public ICollection<Doctor> Doctors { get; set; }
假设这样设置,那么您将通过以下方式查询:
context.Clinics.Where(x => x.Doctors.Any(d => d.Specialties.Any(s => s.SpecialtyId == specialtyId)))
所以每个诊所都有零个或多个医生,每个医生都在零个或多个诊所工作。显然,您使用的框架使用 virtual ICollection
实现这种多对多关系,就像 Entity Framework 那样。
class Clinic
{
public int Id {get; set;} // primary key
... // other properties
// every Clinic has zero or more Doctors (many to many):
public virtual ICollection<Doctor> Doctors {get; set;}
}
同样,医生和专业之间存在多对多关系:
class Doctor
{
public int Id {get; set;} // primary key
... // other properties
// every Doctor works in zero or more Clinics (many-to-many):
public virtual ICollection<Clinic> Clinics {get; set;}
// every Doctor has zero or more Specialties
public virtual ICollection<Specialty> Specialties {get; set;}
}
为了完整起见,专业:
class Specialty
{
public int Id {get; set;} // primary key
public string Description {get; set;}
... // other properties
// every Specialty belongs to zero or more Doctors (many to many):
public virtual ICollection<Doctor> Doctors {get; set;}
}
I want to make a linq query that find all clinics that have doctors with an especific specialty.
int specialtyId = ...
var clinicsWithSpecialty = myDbContext.Clinics
// keep only those Clinics that have at least one Doctor with the desired specialty
.Where(clinic => clinic.Doctors
.Where(doctor => doctor.Specialties.Select(specialty => specialty.Id == specialtyId)
.Any())
.Select(clinic => new
{
// Select the clinic properties you want:
Id = clinic.Id,
Name = clinic.Name,
...
// Select the Doctors you want:
Doctors = clinic.Doctorw.Where(doctor => ...)
.Select(doctor => new
{
// Select the Doctor properties that you want
Id = doctor.Id,
...
Specialties = doctor.Specialties
.Where(specialty => ...)
.Select(specialty => new
{
// Select the specialties that you want
...
})
.ToList(),
})
.ToList(),
});
出于效率原因,不要 select 任何您不打算使用的属性,尤其是连接 table 的外键。如果 Clinic [10] 有 300 名医生,那么每个 Doctor 都将具有相同的结点 table 外键值。不使用这个值,传300次就浪费了
When fetching data, only use Include if you plan to change the fetched data. If you only need to query it, use Select.
我遇到以下问题: 我有一些表 "clinic"、"doctor"、"clinicDoctor"(代表多对多的关系),还有 "specialty" 和 "doctorSpecialty"(代表多对多的关系)关系)。 我想进行一个 linq 查询,以查找所有拥有特定专业医生的诊所。
在简历中,我想按专业 ID 查找诊所
var clinicLst = (from cli in clinica.Clinic
.Include(x => x.ClinicDoctor) //This is a Collection
.ThenInclude(x => x.IdDoctorNavigation)
.ThenInclude(x => x.DoctorSpecialty) //This is a Collection
.ThenInclude(x => x.IdSpecialtyNavigation)
.Where(cli => (cli.State == true)
&& HERE I WANT THE ESPECIALIDAD.ID == ID BUT I DON'T KNOW HOW)
select cli).ToList();
请多多指教。 提前致谢!!!
你的问题不是很清楚,但我认为你正在寻找类似的东西:
var clinicList = await clinica.Clinic.Where(x => x.DoctorSpecialty.Any(s => s.Id == specialtyId)).ToListAsync();
更新
因此 "your question isn't very clear"。通常,对于多对多,尤其是如果您使用的是 EF Core,您应该设置如下:
public class Doctor
{
...
public ICollection<DoctorSpecialty> Specialties { get; set; }
}
public class Specialty
{
...
public ICollection<DoctorSpecialty> Doctors { get; set; }
}
public class DoctorSpecialty
{
public int DoctorId { get; set; }
public int SpecialtyId { get; set; }
}
然后,Clinic
会是这样的:
public ICollection<Doctor> Doctors { get; set; }
假设这样设置,那么您将通过以下方式查询:
context.Clinics.Where(x => x.Doctors.Any(d => d.Specialties.Any(s => s.SpecialtyId == specialtyId)))
所以每个诊所都有零个或多个医生,每个医生都在零个或多个诊所工作。显然,您使用的框架使用 virtual ICollection
实现这种多对多关系,就像 Entity Framework 那样。
class Clinic
{
public int Id {get; set;} // primary key
... // other properties
// every Clinic has zero or more Doctors (many to many):
public virtual ICollection<Doctor> Doctors {get; set;}
}
同样,医生和专业之间存在多对多关系:
class Doctor
{
public int Id {get; set;} // primary key
... // other properties
// every Doctor works in zero or more Clinics (many-to-many):
public virtual ICollection<Clinic> Clinics {get; set;}
// every Doctor has zero or more Specialties
public virtual ICollection<Specialty> Specialties {get; set;}
}
为了完整起见,专业:
class Specialty
{
public int Id {get; set;} // primary key
public string Description {get; set;}
... // other properties
// every Specialty belongs to zero or more Doctors (many to many):
public virtual ICollection<Doctor> Doctors {get; set;}
}
I want to make a linq query that find all clinics that have doctors with an especific specialty.
int specialtyId = ...
var clinicsWithSpecialty = myDbContext.Clinics
// keep only those Clinics that have at least one Doctor with the desired specialty
.Where(clinic => clinic.Doctors
.Where(doctor => doctor.Specialties.Select(specialty => specialty.Id == specialtyId)
.Any())
.Select(clinic => new
{
// Select the clinic properties you want:
Id = clinic.Id,
Name = clinic.Name,
...
// Select the Doctors you want:
Doctors = clinic.Doctorw.Where(doctor => ...)
.Select(doctor => new
{
// Select the Doctor properties that you want
Id = doctor.Id,
...
Specialties = doctor.Specialties
.Where(specialty => ...)
.Select(specialty => new
{
// Select the specialties that you want
...
})
.ToList(),
})
.ToList(),
});
出于效率原因,不要 select 任何您不打算使用的属性,尤其是连接 table 的外键。如果 Clinic [10] 有 300 名医生,那么每个 Doctor 都将具有相同的结点 table 外键值。不使用这个值,传300次就浪费了
When fetching data, only use Include if you plan to change the fetched data. If you only need to query it, use Select.