anthonypliu
anthonypliu

Reputation: 12437

Joining tables in Entity Framework

I have a table called Students and a table called Majors, Students and Majors are joined by MajorId I have set this relationship already and have set the foreign key in the schema. When I access my Student object how can I return the MajorName column (this comes from the Majors table)? The only options I have in intellisense is Major_1, Major_1Reference, MajorId .

Upvotes: 1

Views: 3809

Answers (2)

Amol Kolekar
Amol Kolekar

Reputation: 2325

You can use linq join statement like this to make query on the two tables...

var q = from s in Students
        join m in Majors on s.MajorId  equals m.MajorId 
        select new { m.MajorName }; 

Upvotes: 0

StriplingWarrior
StriplingWarrior

Reputation: 156459

Major_1 should be a navigation property leading to the appropriate Major entry, so you should be able to access the Major's properties like this:

from s in ctx.Students
select s.Major_1.MajorName

Upvotes: 2

Related Questions