sheen drung
sheen drung

Reputation: 73

How to get two table value using Linq

I have two table one is Administrator table and another is Teacher table . I want to display these both tables values in single Gridview . I have make id as a primary key in Administrator table and make this tech_id as foreign key in Teacher table . Now how to get these table values together in single gridview as shown in pic enter image description here

Now please any body help me how to get these two value together using Linq . I have try but I can't make any more

   private void loadgri()
    {
        StudentDatabaseEntities empl = new StudentDatabaseEntities();

      var query=from g in empl.Teachers
                join m in empl.Administrators on g.id equals m.id
                where m.username=="cs"
                select new{
   Name = g.username,


        };
       }

Upvotes: 0

Views: 171

Answers (2)

Evan Huang
Evan Huang

Reputation: 1275

To show all the teachers and their administrator, you don't have to use "join", you could just use the navigation property:

var query = from g in empl.Teachers
            where g.Administrator.username=="cs"
            select new {
                Teacher_Id = g.Id,
                Teacher_Name = g.username,
                Administrator_Id = g.Id,
                Administrator_Name = g.Administrator.username,
                //etc...
            };

Upvotes: 3

Tim Schmelter
Tim Schmelter

Reputation: 460058

You don't need a join if you have already a navigation-property:

var query= from t in empl.Teachers
           where t.Administrator.username == "cs"
           select new { Teacher = t.username, Administrator = t.Administrator.username }; 

This is just an example, but you see that you can access all properties of both entities.

Don’t use Linq’s Join. Navigate!

Upvotes: 5

Related Questions