2014-08-29 106 views
-4

我有这样的:一个具有相同的键项已经添加,从字典

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Net; 
using System.Text; 
using System.Text.RegularExpressions; 
using System.Threading; 
using System.Threading.Tasks; 

namespace ConsoleApplication2 
{ 
    public class Program 
    { 
     static void Main(string[] args) 
     { 

      //Consider making this configurable 
      const string sourceFile = "testSolar.txt"; 

      //const string pattern = "http://10.123.9.66:80"; 
      //var FirstSeparatorLastNameExact = new[] { "nosyn_name_last_exact:(qxq" }; 
      //var SecondSeparatorLastNameExact = new[] { "qxq)" }; 

      string[] FirstSeparator = new string[] { "nosyn_name_last_exact:(qxq" }; 
      string[] SecondSeparator = new string[] { "qxq)" }; 

      string[] FirstSeperatorFirstName = new string[] { "nosyn_name_first_exact:(qxq" }; 
      string[] secondSeperatorFirstName = new string[] { "qxq)" }; 

      string[] nameLastBFirst = new string[] {"nosyn_name_last_b_exact:(qxq" }; 
      string[] nameLastBSecond = new string[] {"qxq)"}; 



      Regex re = new Regex("^(http|https)://"); 
      HttpWebResponse response; 

      // var webClient = new WebClient(); 
      var times = new Dictionary<string, TimeSpan>(); 
      var stopwatch = new System.Diagnostics.Stopwatch(); 

      //Add header so if headers are tracked, it will show it is your application rather than something ambiguous 
      //webClient.Headers.Add(HttpRequestHeader.UserAgent, "Response-Tester-Client"); 

      var urlList = new List<string>(); 
      var listNames = new List<string>(); 

      var firstNames = new Dictionary<string, string>(); 

      //Loop through the lines in the file to get the urls 
      try 
      { 
       stopwatch.Start(); 
       using (var reader = new StreamReader(sourceFile)) 
       { 

        while (!reader.EndOfStream) 
        { 
         //var urNewList = new List<string>(); 
         var line = reader.ReadLine(); 
         var columns = line.Split('\t'); 

         if (columns[2] == "R") 
         { 
          var url = columns[4] + "?" + columns[5]; 
          urlList.Add(url); 
          //Thread.Sleep(250); 
         } 

         var temp = line.Split(FirstSeparator, StringSplitOptions.RemoveEmptyEntries)[1]; 
         var result2 = temp.Split(SecondSeparator, StringSplitOptions.RemoveEmptyEntries)[0]; 
         //Console.WriteLine(result2); 
         listNames.Add(result2); 

          var split = line.Split(FirstSeperatorFirstName, StringSplitOptions.RemoveEmptyEntries); 
          if (split.Length > 1) 
          { 

           var hallo = (split[1].Split(')')[0]); 
           firstNames.Add(result2, hallo); 
          } 
        } 
       } 
      } 

      catch (Exception e) 
      { 
       Console.WriteLine("An error occured while attempting to access the source file at {0}", sourceFile); 
      } 
      finally 
      { 
       //Stop, record and reset the stopwatch 
       stopwatch.Stop(); 
       times.Add("FileReadTime", stopwatch.Elapsed); 
       stopwatch.Reset(); 
      } 

      //Try to connect to each url 
      var counter = 1; 
      foreach (var url in urlList) 
      { 
       try 
       { 
        stopwatch.Start(); 

        using (WebClient webClient = new WebClient()) 
        { 

         webClient.Headers.Add(HttpRequestHeader.UserAgent, "Response-Tester-Client"); 

         // HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 
         HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 
         request.Method = "POST"; 

         //webClient.Dispose(); 
        } 
       } 
       catch (Exception e) 
       { 
        Console.WriteLine("An error occured while attempting to connect to {0}", url); 
       } 
       finally 
       { 
        stopwatch.Stop(); 
        //We use the counter for a friendlier url as the current ones are unwieldly 
        times.Add("Url " + counter, stopwatch.Elapsed); 
        counter++; 

        stopwatch.Reset(); 
       } 
      } 

      //Release the resources for the WebClient 
      //webClient.Dispose(); 

      //Write the response times 
      Console.WriteLine("Url " + "\t\t\t\tLast Name" + "\t\t\t first Name"); 
      int index = -1; 

      foreach (var key in times.Keys) 
      { 
       if (key.Contains("Url")) 
       { 
        index++; 
        var temp = firstNames.ContainsKey(listNames[index]) ? "\t\t" + listNames[index] + "\t\t" + firstNames[listNames[index]] : "\t\t" + listNames[index]; 
        Console.WriteLine("{0}: {1} {2}", key, times[key].TotalSeconds, temp); 
       } 
       else 
       { 
        Console.WriteLine("{0}: {1}", key, times[key].TotalSeconds); 
       } 
      } 
      Console.ReadKey(); 
     } 
    } 
} 

,但我得到这个错误:

An item with the same key has already been added.

从这个:

var hallo = (split[1].Split(')')[0]); 
firstNames.Add(result2, hallo); 

但如何检查价值是否已经在字典中?检查

回答

1

使用ContainsKey方法,如果给定的key已经在字典中

http://msdn.microsoft.com/en-us/library/kw5aaea4(v=vs.110).aspx

+0

你的意思是这样:var分= line.Split(FirstSeperatorFirstName,StringSplitOptions.RemoveEmptyEntries); (split.Length> 1) { var hallo =(split [1] .Split(')')[0]); if(firstNames.ContainsKey(hallo)) { firstNames.Add(result2,hallo); } } – SavantKing 2014-08-29 13:28:34

相关问题