0

OK, so i have my Client table/entity. I also have a Medical table/entity. And because my Client can have multiple Medical records i created a resolving table/entity called ClentMedial which consists of 3 attributes, ID, ClientID and MedicalID.

I have figured out how to create a ClientMedical record when specifying the ID explicitly using the following code:

    ClientMedical cm = new ClientMedical();
    var med = (from m in db.Medicals where m.Id == 1 select m).First();
    cm.Client = client;
    cm.Medical = med;
    client.ClientMedicals.Add(cm);
    db.SaveChanges();

However i don't know how to iterate through the Clients ClientMedical collection to display the medical condition (the Medical entity has a condition attribute). I am new to Entity Framework. I have a Client form which shows all the info about a particular client, what i want to do is to be able to show/add/remove medical conditions for a particular client.

I am sorry if this is a bit vague i tried to explain it as best as i can.

EDIT:

Was able to access the data like this:

 IQueryable<Medical> med =
                from p in db.ClientMedicals
                where p.Client.Id == client.Id
                select p.Medical;

            foreach (Medical m in med)
            {
                MessageBox.Show(m.Condition);
            }

If someone has better way please say.

1 Answers1

0

You want all medical for specific client?

var client = db.Clients.Where(c=>c.ClientID == someID).Include("Medical").Single();

Now in client, list Medical has been populated with medicals for this client.

Senaid
  • 1
  • That did not work. I got the following error: 'System.Linq.IQueryable' does not contain a definition for 'Include' and no extension method 'Include' accepting a first argument of type 'System.Linq.IQueryable' could be found (are you missing a using directive or an assembly reference?). I thought EF was suppose to make things easier haha. –  Sep 22 '13 at 17:07
  • using System.Data.Entity; – Senaid Sep 22 '13 at 17:09
  • It looks this method isn't in IQueryable interface, you must write extension. http://stackoverflow.com/questions/5256692/linq-to-entities-include-method-not-found – Senaid Sep 22 '13 at 17:13
  • I don't really want to have to do that, shortly there is a way using normal LINQ ? This EF stuff is really confusing. –  Sep 22 '13 at 17:16
  • Look at this http://stackoverflow.com/questions/5783109/in-linq-to-sql-how-do-i-include-the-child-entity-with-initial-query – Senaid Sep 22 '13 at 17:18
  • Did not help. Thanks anyway. –  Sep 22 '13 at 17:26