假設你Collection2
項目是在字[沒有雙關語意],你可以使用LINQ ToLookup的通常含義的話 - 這會給你一個適當的MultiValueDictionary模擬,並使用您可以嘗試類似:
var phrases = new[] { "I am good", "He is best", "They are poor", "Mostly they are average", "All are very nice", "Not so\tgood \t", };
var lookup = phrases
.Select((phrase, index) =>
new
{
phrase,
index,
words = phrase.Split((Char[])null, StringSplitOptions.RemoveEmptyEntries)
})
.SelectMany(item =>
item
.words
.Select(word =>
new
{
word,
item.index,
item.phrase
}))
.ToLookup(
keySelector: item => item.word,
elementSelector: item => new { item.phrase, item.index });
var wordsToSearch = new[] { "good", "best", "nice" };
var searchResults = wordsToSearch
.Select(word =>
new
{
word,
phrases = lookup[word].ToArray()
});
foreach (var result in searchResults)
{
Console.WriteLine(
"Word '{0}' can be found in phrases : {1}",
result.word,
String.Join(
", ",
result
.phrases
.Select(phrase =>
String.Format("{0}='{1}'", phrase.index, phrase.phrase))));
}
它提供給你的指標和短語,讓您可以根據需要適應它。
但是,如果您的Collection2
不是由單詞組成,而是由短語組成,那麼您將需要更強大的功能,如lucene.net,這可以正確處理全文搜索。
看起來你需要一個倒排索引,看看Lucene.NET如何做到這一點,或者只是使用該庫。 –
我想知道你是否對這個主題做過任何研究。例如在右邊的相關列中有[什麼.NET集合提供了最快的搜索?](http://stackoverflow.com/questions/1009107/what-net-collection-provides-the-fastest-search? ) – Steve
@EugenePodskal; Collection1項目將會像一個短語。 Collection2項目將是在Collection1短語中搜索的所有單詞,然後將匹配的短語放入Collection3中。 – p0iz0neR