使用公共键将两个表连接起来,并在datagrid C#linq上显示

时间:2018-06-26 21:39:31

标签: c# linq

我需要连接一个DataGrid的两个表,但是我只想要具有相同ID的值(idviagem在表idviagem中为pk,在表idpassageiro中为fk)

我不知道如何执行查询,那一刻我只将表tbpassageiro放在网格上,并且我想在键相等时将它们加入DataGrid上

using (checkinEntities1 db = new checkinEntities1())
{

    var qcheckin = (from c in db.tbpassageiro
                    join g in db.tbviagem on c.idviagem equals g.idviagem
                    where c.idviagem == g.idviagem
                    select c).ToList();

     gridpass.ItemsSource = qcheckin;

}

我知道100%的绑定是正确的(表passiro中的某些值和表tbviagem中的其他Biding值)

这就是我想做的:

enter image description here

1 个答案:

答案 0 :(得分:0)

如果要从两个表中获取列,则必须为要从两个表中获取的列创建视图模型。

using (checkinEntities1 db = new checkinEntities1())

{

var qcheckin = (from c in db.tbpassageiro
                join g in db.tbviagem on c.idviagem equals g.idviagem
                where c.idviagem == g.idviagem
                select new viewModelName()
                {
                    //get the column values here like
                    Hora = c.Hora,
                    Partida = g.Partida
                }).ToList();

 gridpass.ItemsSource = qcheckin;

}

希望这会为您提供所需的答案。