c# - Refactor query in LINQ -


i have query needs check value object list if exists loop through value of main object. how can recode below?

foreach (var kd in dto.keydrivermodels) {   var keydrivermodelnodevalue = result.singleordefault(x => x.keydrivermodelid == kd.id);   kd.selectionstatus = keydrivermodelnodevalue != null ?                           keydrivermodelnodevalue.selectionstatus : string.empty; } 

i tried model code , rewrite code linq. see if works

        static dto dto = new dto();          static void main(string[] args)          {              list<keydriver> keydrivers = new list<keydriver>();              list<keydriver> result = keydrivers.where(x => x.keydrivermodelid > 1000).tolist();                foreach (var kd in dto.keydrivermodels)              {                  var keydrivermodelnodevalue = result.singleordefault(x => x.keydrivermodelid == kd.id);                  kd.selectionstatus = keydrivermodelnodevalue != null ?                                         keydrivermodelnodevalue.selectionstatus : string.empty;              }                //new code              var kd1 = r in result                        join d in dto.keydrivermodels                        on r.keydrivermodelid equals d.id  drs                        dr in drs                        select new { dr }.dr.selectionstatus != null ? dr.selectionstatus : string.empty;          }      }      public class dto      {          public list<keydrivermodel> keydrivermodels { get; set; }      }      public class keydrivermodel      {          public int id { get; set; }          public string selectionstatus { get; set; }      }      public class keydriver      {          public int keydrivermodelid { get; set; }          public string selectionstatus { get; set; }        }  ​


Comments