nob*_*ody 6 c# collections linq-to-objects data-structures
string[] words = System.IO.File.ReadAllLines("word.txt");
var query = from word in words
where word.Length > "abe".Length && word.StartsWith("abe")
select word;
foreach (var w in query.AsParallel())
{
Console.WriteLine(w);
}
Run Code Online (Sandbox Code Playgroud)
基本上word.txt包含170000个英文单词.C#中的集合类是否比上述查询的字符串数组更快?将不会插入或删除,只搜索字符串以"abe"或"abdi"开头.
文件中的每个单词都是唯一的.
EDIT 1在我的应用程序中,此搜索可能会执行数百万次.另外,我想坚持使用LINQ进行集合查询,因为我可能需要使用聚合函数.
EDIT 2 文件中的单词已经排序,文件不会更改
我自己会创建一个Dictionary<char, List<string>>,在其中我会按单词的第一个字母对单词进行分组。这将大大减少所需单词的查找。