让所有的SQL表使用的foreach的值 [英] get all the values in Sql table using foreach
问题描述
我需要从表StaffSectionInCharge让所有的入侵检测系统,它只有两列和STAFFID SectionId,我和STAFFID的StudentId值.....问题是我不能直接获取这些记录此表.....我使用实体框架,此表的设计
[EdmRelationshipNavigationPropertyAttribute(模型,StaffSectionInCharge,科)]
公共EntityCollection<节>第
{
得到
{
返回((IEntityWithRelationships)this).RelationshipManager.GetRelatedCollection<Section>(\"Model.StaffSectionInCharge\", 部分);
}
组
{
如果((价值!= NULL))
{
((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedCollection<Section>(\"Model.StaffSectionInCharge\", 节,值);
}
}
}
我可以像
通过对员工表访问此表 员工员工= buDataEntities.Staffs.First(S = GT; s.StaffId STAFFID ==);
第节= buDataEntities.Sections.First(S = GT; s.SectionId == SectionId);staff.Sections.Add(部分);
buDataEntities.savechanges();
这样我可以记录添加到该StaffSectionInCharge表....
在这里我要得到相应的SectionId所有StaffIds
我试图让像
DataAccess.Staff人员=新DataAccess.Staff();的foreach(在staffs.Sections.Select INT人员(S = GT; s.SectionId))
{ }
但它没有工作,任何人都可以帮助我在这里
VAR staffIds = buDataEntities.Staffs
。凡(ST = GT; st.Sections.Any(SE = GT; se.SectionId == SectionId))
。选择(ST = GT; st.StaffId)
.ToList();
或
VAR staffIds = buDataEntities.Sections
。凡(SE = GT; se.SectionId == SectionId)
.SelectMany(SE = GT; se.Staffs.Select(ST = GT; st.StaffId))
。不同()
.ToList();
这两个选项应该工作。如果 SectionId
是部分
的主键可以smplify第二code为:
VAR staffIds = buDataEntities.Sections
。凡(SE = GT; se.SectionId == SectionId)
。选择(SE = GT; se.Staffs.Select(ST = GT; st.StaffId))
.SingleOrDefault();
I need to get all the Ids' from the table "StaffSectionInCharge", it has only two columns StaffId and SectionId, I have values of StaffId and StudentId.....the problem is I can't get the records directly to this table.....am using entity framework and the design of this table is
[EdmRelationshipNavigationPropertyAttribute("Model", "StaffSectionInCharge", "Section")]
public EntityCollection<Section> Sections
{
get
{
return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedCollection<Section>("Model.StaffSectionInCharge", "Section");
}
set
{
if ((value != null))
{
((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedCollection<Section>("Model.StaffSectionInCharge", "Section", value);
}
}
}
I can access this table through staff table like
Staff staff = buDataEntities.Staffs.First(s => s.StaffId == StaffId);
Section section = buDataEntities.Sections.First(s => s.SectionId == SectionId);
staff.Sections.Add(section);
buDataEntities.savechanges();
like this I can add the records to this StaffSectionInCharge table....
here I want to get all the StaffIds for the corresponding SectionId
I tried getting like
DataAccess.Staff staffs = new DataAccess.Staff();
foreach (int staff in staffs.Sections.Select(s=>s.SectionId))
{
}
but its not working, can anyone help me here
var staffIds = buDataEntities.Staffs
.Where(st => st.Sections.Any(se => se.SectionId == SectionId))
.Select(st => st.StaffId)
.ToList();
or
var staffIds = buDataEntities.Sections
.Where(se => se.SectionId == SectionId)
.SelectMany(se => se.Staffs.Select(st => st.StaffId))
.Distinct()
.ToList();
Both options should work. If SectionId
is the primary key of Section
you can smplify the second code to:
var staffIds = buDataEntities.Sections
.Where(se => se.SectionId == SectionId)
.Select(se => se.Staffs.Select(st => st.StaffId))
.SingleOrDefault();
这篇关于让所有的SQL表使用的foreach的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!