hncl
hncl

Reputation: 2295

C# EF List from two classes

I have two classes:

   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; }
    }

I am using the following to get two values from these models using:

  var vaccination = db.Alergies.Where(x => x.PHN == phn)
  .Select(x => new { VaccinationAlergyID= x.VaccinationAlergyID,
   VaccinationAlergyName= x.VaccinationAlergy.VaccinationAlergyName}).ToList();

I get only the first VaccinationAlergyName the rest are the id of VaccinationAlergyName.
Would appreciate your suggestions.

Upvotes: 0

Views: 57

Answers (1)

Kyle Huang
Kyle Huang

Reputation: 451

Try

 var vaccination = db.Alergies.Where(x => x.PHN == phn).Include(x=> x.VaccinationAlergy).Select(x => new { VaccinationAlergyID= x.VaccinationAlergyID,VaccinationAlergyName=x.VaccinationAlergy.VaccinationAlergyName}).ToList();

Upvotes: 2

Related Questions