2013-02-23 50 views
0

我有10000+的unicode記錄在我的txt文件,我使用這個功能從TXT文件搜索記錄:如何從WP7中的大量記錄的Txt文件中搜索記錄?

Dictionary<string, string> myLookupTable = new Dictionary<string, string>(); 

    private void LoadFile(){ 

    var resource = Application.GetResourceStream(new Uri("datatxt.txt", UriKind.Relative)); 

    StreamReader streamReader = new StreamReader(resource.Stream,System.Text.Encoding.Unicode); 

       string line; 
       char[] spaceSeparator = new char[] { ',' 
       string[] result; 

       while (!streamReader.EndOfStream) 
       { 
        line = streamReader.ReadLine(); 
        result = line.Split(spaceSeparator, StringSplitOptions.None); 
        myLookupTable.Add(result[0],result[1]); 
       } 
    } 

它說「值沒有在預期範圍之內。」從「myLookupTable.Add(result [0],result [1]);」

任何人都知道這個錯誤是什麼原因,我該如何解決它?

非常感謝!

+0

是否有保證的是:結果= line.Split(spaceSeparator,StringSplitOptions.None);返回2個結果,我的意思是結果[0]和結果[1]不爲空? – 2013-02-23 06:34:44

+0

是的,兩個結果將返回...當記錄減少到> 100時,此功能工作正常.. – 2013-02-23 11:35:38

回答

1

而不是使用

myLookupTable.Add(result[0],result[1]); 

,你可以使用

if(!myLookupTable.ContainsKey(result[0])) 
{ 
    myLookupTable.Add(result[0],result[1]); 
} 
else 
{ 
    //You have to implement this based on your application business rules 
} 
+0

對不起。我不熟練編程,你可以\t 註釋你的答案??非常感謝! – 2013-02-23 15:09:51

+0

只需在我的答案中用代碼中的第二部分替換我突出顯示的第一行。 – 2013-02-23 18:45:19