c# - How can I take objects from the second set of objects which don't exist in the first set of objects in fast way? -


i have records in 2 databases. entity in first database:

public class personindatabaseone {     public string name { get; set; }     public string surname { get; set; } } 

that entity in second database:

public class personindatabasetwo {     public string firstname { get; set; }     public string lastname { get; set; } } 

how can records second database don't exist in first database (the first name , last name must different in first database). have slow, slow:

list<personindatabaseone> peopleindatabaseone = new list<personindatabaseone>(); // hear generate objects in real take database: (int = 0; < 100000; i++) {     peopleindatabaseone.add(new personindatabaseone { name = "aaa" + i, surname = "aaa" + }); }   list<personindatabasetwo> peopleindatabasetwo = new list<personindatabasetwo>(); // hear generate objects in real take database: (int = 0; < 10000; i++) {     peopleindatabasetwo.add(new personindatabasetwo { firstname = "aaa" + i, lastname = "aaa" + }); } (int = 0; < 10000; i++) {     peopleindatabasetwo.add(new personindatabasetwo { firstname = "bbb" + i, lastname = "bbb" + }); }   list<personindatabasetwo> peopleindatabasetwowhichnotexistindatabaseone = new list<personindatabasetwo>();  // below code slow: foreach (personindatabasetwo personindatabasetwo in peopleindatabasetwo) {     if (!peopleindatabaseone.any(x => x.name == personindatabasetwo.firstname && x.surname == personindatabasetwo.lastname))     {         peopleindatabasetwowhichnotexistindatabaseone.add(personindatabasetwo);     } }; 

edit: respect comments - using real model , dictionary instead of simple set:

try hashing list dictionary hold people objects, key - try tuple instead of name1==name2 && lname1==lname2.

this potentially this:

// people1 , people2 lists of models exist: var sw = stopwatch.startnew(); var removethese = people1.select(x=>tuple.create(x.firstname,x.lastname)); var dic2 = people2.todictionary(x=>tuple.create(x.name,x.surname),x=>x); var result =  dic2.keys.except(removethese).select(x=>dic2[x]).tolist(); console.writeline(sw.elapsed); 

i hope helps.


Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -