获得外键表中的值 [英] Get values from foreign key table

查看:134
本文介绍了获得外键表中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用实体框架的数据库与我的asp.net应用程序连接

PM。在这里,我有一个外键表里面有两列,和STAFFID SectionId 。在这里, STAFFID是员工表和 SectionId 的主键是表的主键。我有一个像

在此表中的值

  STAFFID是SectionId
--------- -------
  1 1
  2 5
  5 8
  1 5
  1 8

在这里我认识了 STAFFID是,我需要让所有的 SectionIds 此对应的STAFFID是(例如1 ,5和8在这里工作人员1)。

如果我想知道用首先方法,与已知我STAFFID可以做喜欢的细节

  DataObject.Entities dataEntities =新DataObject.Entities();DataObject.Section节= dataEntities.Sections.First(S = GT; s.Staffs
                                  。选择(SS => ss.StaffId)。载有(STAFFID是));

这个我可以得到有关第一节,与 STAFFID是(例如相匹配的信息。信息约sectionid = 1这里)

在以同样的方式我试图让所有的sectionIds特定像STAFFID,

 列表< INT> sectionIds = dataEntities.Sections.Where(S => s.Staffs.Where
                       (SS => ss.StaffId STAFFID ==))选择(秒=> sec.SectionId);

但它没有工作,任何人都可以帮助我在这里


解决方案

  

在这里,我知道和STAFFID我需要得到这一切的SectionIds
  STAFFID相应


 列表< INT> sectionIds = dataEntities.Sections
    。凡(SE = GT; se.Staffs.Any(ST = GT; st.StaffId STAFFID ==))
    。选择(SE = GT; se.SectionId)
    .ToList();

Am using Entity framework to connect database with my asp.net application. Here I've a Foreign key table which has two columns, StaffId and SectionId. Here StaffId is the Primary key of Staff table and SectionId is the Primary key of Sections table. I have values in this table like

StaffId     SectionId
-------     ---------
  1            1
  2            5
  5            8
  1            5
  1            8

here I know the StaffId and I need to get all the SectionIds for this corresponding staffid(eg. 1,5 and 8 for staff 1 here).

If I want to know the details with First method, with the known StaffId I can do like,

DataObject.Entities dataEntities=new DataObject.Entities();

DataObject.Section section = dataEntities.Sections.First(s=>s.Staffs
                                  .Select(ss=>ss.StaffId).Contains(staffId));

with this I can get the information about the first section that matches with the StaffId.(ex:info about sectionid=1 here)

In the same way I tried to get all the sectionIds for a particular staffId like,

List<int> sectionIds = dataEntities.Sections.Where(s => s.Staffs.Where
                       (ss => ss.StaffId == staffId)).Select(sec=>sec.SectionId);

but its not working, can anyone help me here

解决方案

here I know the StaffId and I need to get all the SectionIds for this corresponding staffid

List<int> sectionIds = dataEntities.Sections
    .Where(se => se.Staffs.Any(st => st.StaffId == staffId))
    .Select(se => se.SectionId)
    .ToList();

这篇关于获得外键表中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆