title: how do I update TFS test results by configuration?
body: <p>I have a method that seems to be working to update a TFS test result ONLY for the configuration that it ran on.  But it seems very inefficient.  Can someone help me to figure out what I'm doing wrong? - specifically it doesn't seem that I should have to get testSuites for a given test ID, then iterate through all tests for those suites to compare to the test ID I already have. </p>
  
  <p>As an example, I run a test against Windows 8/Chrome.  So TestContext contains the appropriate values that map to a configuration.  I want to update the test result for that configuration in every test suite that it exists in.  Here is the code:</p>
  
  <pre><code>    public void UpdateResult(TestContext context, bool passed)
      {
          int testCaseID = Convert.ToInt32(context.Test.Properties["id"]);
  
          TfsTeamProjectCollection projectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(tfsUri);
  
          ITestManagementService service = projectCollection.GetService&lt;ITestManagementService&gt;();
          ITestManagementTeamProject teamProject = service.GetTeamProject(teamProjectName);
  
          ITestCase testCase = teamProject.TestCases.Find(testCaseID);
          ITestSuiteCollection testSuites = teamProject.TestSuites.ReferencingTestCase(testCaseID);
  
          if (testCase != null)
          {
              // ---- Get Test Plan
              int testPlanId = 20589;
              ITestPlan testPlan = teamProject.TestPlans.Find(testPlanId);
  
              // ---- Create Test Run
              ITestRun testRun = teamProject.TestRuns.Create();
              testRun = testPlan.CreateTestRun(true);
  
              // ---- Get TestPoint
              ITestPointCollection points = testPlan.QueryTestPoints("SELECT * FROM TestPoint WHERE TestCaseId =" + testCaseID);
              foreach (ITestPoint tp in points)
              {
                  //Get configuration, which contains configuration values
                  ITestConfiguration testConfiguration = teamProject.TestConfigurations.Query("Select * from TestConfiguration WHERE Name='" + tp.ConfigurationName + "'")[0];
                  IDictionary&lt;string, string&gt; testConfigValues = testConfiguration.Values;
  
                  if ((!testConfigValues.ContainsKey("Browser") ||
                      testConfigValues["Browser"].ToLower() == context.Test.Properties["Browser"].ToString().ToLower())
                      &amp;&amp; (!testConfigValues.ContainsKey("Operating System") ||
                      testConfigValues["Operating System"].ToLower() == context.Test.Properties["Operating System"].ToString().ToLower())
                     )
                  {
                      testRun.AddTestPoint(tp, testPlan.Owner);
                  }
  
              }
  
              testRun.State = TestRunState.Completed;
              testRun.Save();
  
              foreach (ITestSuiteBase testSuite in testSuites)
              {
                  foreach (ITestSuiteEntry testCse in testSuite.TestCases)
                  {
                      ITestCaseResultCollection results = testRun.QueryResults();
                      if (testCse.TestCase.Id == testCaseID)
                      {
  
                          foreach (IdAndName config in testCse.Configurations)
                          {
                              IEnumerable&lt;ITestCaseResult&gt; testCaseResults = from tr in results
                                                                             where
                                                                             tr.TestConfigurationId == config.Id &amp;&amp;
                                                                             tr.TestCaseId == testCase.Id
                                                                             select tr;
  
                              foreach (ITestCaseResult result in testCaseResults)
                              {
                                  result.Outcome = passed ? TestOutcome.Passed : TestOutcome.Failed;
                                  result.State = TestResultState.Completed;
                                  result.Save(true);
                              }
                          }
                      }
  
  
                  }
              }
  
              testRun.Save();
              testRun.Refresh();
          }
      }
  </code></pre>