c# - Most efficient way to compare two IEnumerables (or lists) in Linq? -


i've come across few times , feel i'm writing unessasary code. i'm looking efficient way compare value of field in list list.

for example, let's have viewmodel such following:

    public class usercontractadddto     {         public string contractid { get; set; }         public bool contractselected { get; set; }         public string username { get; set; }     } 

i put values of table view model, , pass ienumerable:

 ienumerable<usercontractadddto> jsonarray 

now have ienumerable of usercontractadddto's, need loop through compare values exist in database.

let's contractselected has been updated of rows, it's set true. use foreach loop, need know if row exists avoid duplicate rows.

        foreach (usercontractadddto u in jsonarray)         {             var = u.contractid.tostring();             if (u.contractselected == true)             {                 foreach (userplansponsorcontract in usercontractlist)                 {                     if (up.plansponsorcontractid.tostring() ==  && up.userid == userid)                     {                         //update row                         var row = _context.userplansponsorcontracts.where(r => r.plansponsorcontractid.tostring() == u.contractid && r.userid == userid).firstordefault();                         row.isverified = true;                         _context.savechanges();                     }                     else                     {                         //add row                         _context.userplansponsorcontracts.add(new userplansponsorcontract                         {                             userid = userid,                             plansponsorcontractid = convert.toint32(u.contractid),                             isverified = true,                             contractadminemailsent = false,                             applieddate = datetime.now,                             reverifyreminder = 0                         });                         _context.savechanges();                     }                 }             }             else             {             foreach (userplansponsorcontract in usercontractlist)             {                 if (up.plansponsorcontractid.tostring() == && up.userid == userid)                 {                     //update row                     var row = _context.userplansponsorcontracts.where(r => r.plansponsorcontractid.tostring() == u.contractid && r.userid == userid).firstordefault();                     row.isverified = false;                     _context.savechanges();                 }                 }             }         } 

this 1 approach i've tried, i'm looking more efficient way of doing it. thoughts?

as general rule, approach need take heavily depends on structure of program. comes down reference type equality vs value equality.

if objects in both lists are, in fact, same object, following:

var diff = mylist.except(myotherlist).tolist(); 

if, in case, dealing value equality (two different objects, same "value"), first need tell c# makes 2 instances of object equal. need define equality.

for doing there 2 schools of thought. can modify existing class , make implement iequatable, or can create brand new class implements iequalitycomparer, whos job compare instances of class.

you can see detailed example of former approach in this answer prashanth thurairatnam, , 1 of latter approach in this answer jon skeet.

in both cases, have implement 2 methods, equals(t, t) , gethashcode(t). these methods used figure out makes 2 instances of object equal. once have done code need have written above (the difference being have provide comparer argument if go iequalitycomparer approach).


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -