Reem Mattar
Reem Mattar

Reputation: 11

How can i join two table in Linq and display the join to Data GridView?

I'm trying to join two tables and to display the Join result into GridView in WinForms, but something is going wrong...

it's not giving me Error message or something, Please help!!

my code :

var temp = teacherCmbBx.SelectedItem.ToString();

var temp2 = (from c in context.Teachers
             where temp == c.FirstName
             select c).ToList();
long num = temp2[0].ID;

var teacherGroup = (from t in context.Teachers
                    join g in context.Groups on t.ID equals g.TeacherID
                    where num == t.ID
                    select t);

teachergrpGridView.DataSource = teacherGroup;

string temp3 = (string)teachergrpGridView.Rows[rowNum].Cells[0].Value;

Upvotes: 1

Views: 2466

Answers (2)

Suraj Singh
Suraj Singh

Reputation: 4059

 var teacherGroup = from t in context.Teachers
                                join g in context.Groups on t.ID equals g.TeacherID
                                where num == t.ID
                                select new {
                                  t.Id ,
                                  g.Name,
                                  t.xxxx 
                                 };

Upvotes: 0

Satpal
Satpal

Reputation: 133403

You are almost there. You are just missing .ToList()

var teacherGroup = (from t in context.Teachers
                    join g in context.Groups on t.ID equals g.TeacherID
                    where num == t.ID
                    select t).ToList();

teachergrpGridView.DataSource = teacherGroup;

Upvotes: 1

Related Questions