c# - How can I create set contain id record in first set equal id record in second set? -


in database have 2 tables:

public partial class personone {     public int id { get; set; }     public string name { get; set; }     public string surname { get; set; } }  public partial class persontwo {     public int id { get; set; }     public string firstname { get; set; }     public string lastname { get; set; } } 

i fill set:

public class persononepersontwo {     public int persononeid { get; set; }     public int persontwoid { get; set; } } 

where personone.name == persontwo.firstname && personone.surname == persontwo.lastname have no idea how can - because below code isn't efficient , slow:

    list<personone> persononelist = new list<personone>();     list<persontwo> persontwolist = new list<persontwo>();      list<persononepersontwo> persononepersontwolist = new list<persononepersontwo>();  foreach (personone personone in persononelist) {     foreach(persontwo persontwo in persontwolist.where(x => x.firstname == personone.name && x.lastname == personone.surname).tolist())     {         persononepersontwolist.add(new persononepersontwo             {                 persononeid = personone.id,                 persontwoid = persontwo.id             });     } }; 

try this:

var result = persononelist.join              (                 persontwolist,                  person1 => new { key1 = person1.name, key2 = person1.surname },                  person2 => new { key1 = person2.firstname, key2 = person2.lastname },                  (person1, person2) => new persononepersontwo { persononeid = person1.id, persontwoid = person2.id }              ).tolist(); 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -