Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在三个系列中使用LINQ?_C#_Linq - Fatal编程技术网

C# 如何在三个系列中使用LINQ?

C# 如何在三个系列中使用LINQ?,c#,linq,C#,Linq,我有三个集合:候选列表、组列表和职位列表。 我想试着找出某个职位上是否有一位候选人,以及在该职位的特定团队中是否有一位候选人。 是这样的: candidateList = RetrieveCandidates(); groupList = RetrieveGroups(); positionList = RetrievePositions(); //first I loop through the candidates if there is at //least 1 candidate p

我有三个集合:候选列表、组列表和职位列表。 我想试着找出某个职位上是否有一位候选人,以及在该职位的特定团队中是否有一位候选人。 是这样的:

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
        }
    }
}
有没有办法简化代码?最好是林克

编辑:我忘了提到独立候选人(candidate.CandidateId==0)


将从这两个列表中筛选至少有一个职位和组的候选人

如果第二个陈述是真的,那么你不需要第一个陈述,如果每个小组至少有一个候选人有一个职位,那么至少有一个候选人有这个职位,所以你只需要确认你的第二个陈述

positionList.All(pos=>
        groupList.All(grp=>
           canidateList.Any(can=> can.PositionId==pos.PositionId 
                                  && can.GroupId == grp.GroupId))); 
但是,请注意,您也在为这两种不同的情况执行例外,这样您就可以创建一个匿名对象来选择不匹配的组,并检查候选组

var result = 
    positionList.Select(pos=>new { 
        Position = pos,
        DoesNotHaveOneCanidate = 
            !canidateList.Any(can=> can.PositionId==pos.PositionId), 
        GroupsMissing = 
            groupList.Where(grp=>
                !canidateList.Any(can=> can.PositionId==pos.PositionId 
                                     && can.GroupId == grp.GroupId)
           ) 
    }); 

您的第一张支票可以减少到:

var exists = positionList.All(p=> candidateList.Any(c=>c.PositionId == p.PositionId));
从那里我们可以创建您的第二张支票作为

exists = groups.All(
                g =>positionList.All(
                     p=> candidateList.Any(
                          c=>c.PositionId == p.PositionId && c.GroupId == g.GroupId)));
exists = groups.All(
                g =>positionList.All(
                     p=> candidateList.Any(
                          c=>c.PositionId == p.PositionId && c.GroupId == g.GroupId)));