获取在c#中链接的外键表的值

时间:2016-05-30 10:39:45

标签: c# mysql database entity-framework foreign-keys

我对数据库中的外键有疑问。我使用Entity framework(visual studio winforms)在c#中进行编程,并且我的sql数据库中包含带有外键的数据。

我有查询来访问这些数据,以便在Datagrid中获取这些数据。一切都很好,除了表中的数据是外键(数字)。当我用查询选择它时,我只得到外键(一个数字),而不是另一个表中链接的值。

var requete_reservations = from reservation_spa in bdd.reservation_spa
                           where reservation_spa.NOMBRE_RESERVATION > 0
                           select new
                           {
                              reservation_spa.CLIENT,
                              reservation_spa.SPA,
                              reservation_spa.NOMBRE_RESERVATION                                            
                           };
dataGrid_reservations.DataSource = requete_reservations.ToList();

reservation_spa.client中,我有一个链接另一个表格客户端的号码

reservation_spa Table

Client table with linked key

如何使用reservation_spa中的外键从客户端获取名称

2 个答案:

答案 0 :(得分:1)

您必须加入表reservation_spaClient,如下所示:

var requete_reservations = from r in bdd.reservation_spa
                           join c in bdd.client on r.CLIENT equals c.IDCLIENT
                            where r.NOMBRE_RESERVATION > 0
                            select new
                            {
                                   c.NOM,
                                   r.SPA,
                                   r.NOMBRE_RESERVATION 
                            };

答案 1 :(得分:0)

名称在哪里?如果您只需要名称,可以使用linq

var name= from c in bdd.Clients //Is that the name of the table of clients?
          where c.IDClient= requete_reservations.Client
          select c;