获得外键表中的值 [英] Get values from foreign key table
问题描述
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
. HereStaffId
is the Primary key ofStaff
table andSectionId
is the Primary key ofSections
table. I have values in this table likeStaffId SectionId ------- --------- 1 1 2 5 5 8 1 5 1 8
here I know the
StaffId
and I need to get all theSectionIds
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 theStaffId
.(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屋!