title: get and set value of object in List in C#
body: <p>I have a list of <code>objects</code>, and I need to search if a value exists in the list, if it does, I need to set one of the properties of the <code>object</code> found to another value, is it possible to do this without searching the <code>List</code> twice? (Once to check if the <code>object</code> exists in the list and once to set its value)</p>
  
  <p>For example:</p>
  
  <pre><code>class myObject
  {
     public string FieldNumber {get;set;}
     public string FieldValue  {get;set;}
  }
  
  var myValues = new List&lt;myObject&gt;();
  myValues.Add(new myObject{ FieldNumber = "100A"});
  myValues.Add(new myObject{ FieldNumber = "100B"});
  
  var allValues = new List&lt;myObject&gt;();
  allValues.Add(new myObject{ FieldNumber = "200A", FieldValue = "3"});
  allValues.Add(new myObject{ FieldNumber = "100B", FieldValue = "C"});
  
  foreach(var obj in allValues)
  {
      GetAndSetField(myValues, obj.FieldNumber, obj.FieldValue);
  }
  
  void GetAndSetField(List&lt;myObject&gt; list, string fieldNumber, string fieldValue)
  {
      var tempField = list.FirstOrDefault(x =&gt; x.FieldNumber.Equals(fieldNumber));
      if(tempField != null)
          list.FirstOrDefault(x =&gt; x.FieldNumber.Equals(fieldNumber)).FieldValue = fieldValue;
  }
  </code></pre>
  
  <p>Is it possible to change the code in the <code>GetAndSetField</code> method so that it doesn't search the list twice if the <code>object</code> is found? 
  Or maybe I'm doing this completely wrong and there's an easier way...</p>