Search code examples
vb.netentity-frameworklinq-to-entitiesnavigation-properties

Entity framework - LINQ JOIN not working


I have a gridview displaying Patient data but I need to use two tables - Currently I am binding it with only the Patient table and its showing Gender, Ethnicity etc ( with numbers ) but I need to get those data from the Ref table. I tried to do join then bind it but I did not work and I was told Navigation property will do the Job but not single tutorial helped me.

Help and advise will be appreciated.

Example of Patient and Ref table

Patient Table 

Patient ID    Patient Name    Gender     Ethnicity   

    1          John            0           1

Ref Table 

 Reference ID    Domain      Code    Description

       1         Gender       0       Male
       2         Gender       1      Female
       3         Ethnicity    1      White
       4         Ethnicity    2      Black

and here is the Join that I tried

   Dim Testing = from x In db.Patient, y In db.ref where y.Domain = "Gender" 
   or y.Domain = "Ethnicity"
   Select x.PatientID, x.PatientName, y.Gender, y.Ethnicity

   gvPatient.DataSource = Testing
   gvPatient.DataBind() 

Solution

  • Create views for each of the lookups like.

    CREATE VIEW Gender 
    AS 
    SELECT Code, Description 
    FROM .Ref 
    WHERE Domain = 'Gender'
    

    Then create entities for each lookup, marking the Code property with a KeyAttribute.