如何在Linq中连接两个表并显示到Data GridView的连接?

时间:2013-09-10 14:19:06

标签: c# linq gridview

我正在尝试连接两个表并在WinForms中将Join结果显示到GridView中,但出现了问题......

它没有给我错误信息或其他什么,请帮助!!

我的代码:

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;

2 个答案:

答案 0 :(得分:1)

你快到了。您只是缺少.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;

答案 1 :(得分:0)

 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 
                                 };