如何在WP7中从具有大量记录的Txt文件中搜索记录?

时间:2013-02-23 06:05:17

标签: c# windows-phone-7 streamreader

我的txt文件中有10000+ unicode记录,我使用此函数从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]);”

任何人都知道这个错误的原因是什么,我该如何解决?

非常感谢!!

1 个答案:

答案 0 :(得分: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
}
相关问题