Wahid Masud
Wahid Masud

Reputation: 1093

Find duplicates between a list and a db table

I want to find the duplicate values between a list and a db table with 1 db call. The list contains 1000+ rows and db table contains 100k+ rows. Is it possible?

The list structure:

public class BatchInvitationDto
{
    public List<Candidates> Candidate { get; set; }
    public string InterviewId { get; set; }
}

public class Candidates
{
    public string Name { get; set; }
    public string Email { get; set; }
}  

and the db structure:

public class Invitations
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [Required(ErrorMessage = "Interview ID is required")]
    public int InterviewId { get; set; }  

    [Required(ErrorMessage = "Candidate Name is Required")]
    public string CandidateName { get; set; }

    [Required(ErrorMessage = "Candidate Email is Required")]
    public string CandidateEmail { get; set; }

    [StringLength(450)]
    [Index(IsUnique = true)]
    public string Guid { get; set; }

}  

now I have 1000+ rows in the list

BatchInvitationDto batchInvites = new BatchInvitationDto(); 
batchInvites.Candidate  // contains 1000+ rows  

and I have 100k+ rows in the db table context.Invitations. Can I check for duplicate emails (emails that exists both in the db and the list variable) with one call? Or what is the best way to do that?

Upvotes: 1

Views: 60

Answers (1)

Amir Molaei
Amir Molaei

Reputation: 3830

Duplicates regarding Email:

var lst = batchInvites.Candidate.Select(y => y.Email);
db.Invitations
    .Where(x => lst.Contains(x.Email))
    .ToList();

Upvotes: 1

Related Questions