我有两个班级:
public partial class Alergy
{
public int ID { get; set; }
public Nullable<int> VaccinationAlergyID { get; set; }
public string PHN { get; set; }
public virtual VaccinationAlergy VaccinationAlergy { get; set; }
}
public partial class VaccinationAlergy
{
public VaccinationAlergy()
{
this.Alergies = new HashSet<Alergy>();
}
public int VaccinationAlergyID { get; set; }
public string VaccinationAlergyName { get; set; }
public virtual ICollection<Alergy> Alergies { get; set; }
}
我使用以下方法从这些模型中获取两个值:
var vaccination = db.Alergies.Where(x => x.PHN == phn)
.Select(x => new { VaccinationAlergyID= x.VaccinationAlergyID,
VaccinationAlergyName= x.VaccinationAlergy.VaccinationAlergyName}).ToList();
我只获得第一个VaccinationAlergyName,其余的是VaccinationAlergyName的ID。
非常感谢您的建议。
答案 0 :(得分:2)
尝试
var vaccination = db.Alergies.Where(x => x.PHN == phn).Include(x=> x.VaccinationAlergy).Select(x => new { VaccinationAlergyID= x.VaccinationAlergyID,VaccinationAlergyName=x.VaccinationAlergy.VaccinationAlergyName}).ToList();