我有三个集合:CandidateList,GroupList和PositionList。 我想知道在某个位置是否有单个候选人,以及该位置是否有该特定组的候选人。 它是这样的:如何在三个集合中进行LINQ?
candidateList = RetrieveCandidates();
groupList = RetrieveGroups();
positionList = RetrievePositions();
//first I loop through the candidates if there is at
//least 1 candidate per position, INCLUDING THE INDEPENDENT CANDIDATES.
foreach (var pos in positionList)
{
bool exists = candidateList.Any(x => x.PositionId == pos.PositionId)
if(!exists)
{
//throw exception
}
}
//then I loop through the groups if there is at least 1 candidate per position.
//This will make sure that all positions for each group has a member.
foreach (var grp in groupList)
{
foreach (var pos in positionList)
{
bool exists = candidateList.Any(x => x.PositionId == pos.PositionId && x.GroupId == grp.GroupId)
if(!exists)
{
//throw exception
}
}
}
是有,我可以简化代码的方法吗?优选LINQ
编辑:我忘了提关于谁是独立的候选人(candidate.CandidateId == 0)