在2个列表之间寻找唯一值

时间:2019-02-27 13:25:21

标签: c# linq

我可以使用linq这样在2个列表之间找到重复的值:

public BatchInvitationDto FindDuplicateInvitations(BatchInvitationDto invitations)
    {
        using (var context = new InterviewEntities())
        {
            var emails = invitations.Candidate.Select(x => x.Email);

            var candidates = context.Invitations
                .Where(x => emails.Contains(x.CandidateEmail) && invitations.InterviewId == x.InterviewId)
                .Select(x => new Candidates
                {
                    Name = x.CandidateName,
                    Email = x.CandidateEmail
                })
                .Distinct()
                .ToList();
            return new BatchInvitationDto
            {
                InterviewId = invitations.InterviewId,
                Candidate = candidates
            };
        }
    }  

但是我找不到2个列表之间的唯一值。所以我要做的是运行一个巨大的循环来查找和删除重复的值。但是这种循环就像永恒一样。有没有办法将下面的代码转换为linq?

public BatchInvitationDto FindUniqueInvitations(BatchInvitationDto invitations)
    {
        using (var context = new InterviewEntities())
        {
            var currentInvitations = context.Invitations.ToList();
            foreach (var currentInvitation in currentInvitations)
            {
                foreach (var newInvitation in invitations.Candidate)
                {
                    if (newInvitation.Email == currentInvitation.CandidateEmail)
                    {
                        invitations.Candidate.Remove(newInvitation);
                        break;
                    }

                }
            }
            return invitations;
        }
    }  

我想用linq做到这一点。

0 个答案:

没有答案