PredicateBuilderを使用して「OrWhere」句をLINQステートメントに動的に追加するのに問題があります。私が最初に達成しようとしていることを説明します。
一連のリンクのタイトルからのキーワードを格納する転置インデックスがあります。私は1つを使用しているので、これらのキーワードに基づいてこれらのリンクをすばやく検索できます。転置インデックスはタイプ
Dictionary<string, List<VerifiedUrl>>
したがって、基本的にすべての単語は、その単語を含むURLのリストに関連付けられています。
次の段階は、転置インデックスを検索可能にすることです。したがって、「青」を検索すると、キー「the」または「blue」に関連付けられているすべてのリンクが返されます。数回のGoogle検索の後、LINQステートメントに「OrWhere」句を動的に追加する最良の方法は、PredicateBuilderクラスを使用することであるように思われました。作成した述語を使用する最後のステップで問題が発生しています。
var invertedIndex = new Dictionary<string, List<VerifiedUrl>>();
//the invertedIndex is built and filled here. Now I am trying to search through it.
Console.WriteLine("\nEnter words to see if there are matches");
string query = Console.ReadLine();
Console.WriteLine();
string[] words = query.Split(',', ' ');
//the predicate I'll be building. I'm not positive it is of the correct type. I've assumed that it should be of the same type as the Dictionary type in the inverted index
var predicate = PredicateBuilder.False<KeyValuePair<string, List<VerifiedUrl>>>();
foreach (string w in words)
{
string temp = w;
predicate = predicate.Or(p => p.Key == temp);
}
//this is the line that generates the syntax error.
test = invertedIndex.Where(predicate);
.Whereステートメントでエラーが発生します。.Whereにカーソルを合わせると、「使用法から型引数を推測できません。型引数を正確に指定してみてください。」と表示されます。
私は変更してみました:
var predicate = PredicateBuilder.False<KeyValuePair<string, List<VerifiedUrl>>>();
に
Expression<Func<KeyValuePair<string, List<VerifiedUrl>>, bool>> predicate = PredicateBuilder.False<KeyValuePair<string, List<VerifiedUrl>>>();
しかし、これは効果がありませんでした。エラーコンソールでは、実際にはさまざまなエラーが発生します。
Error 1 Instance argument: cannot convert from 'System.Collections.Generic.Dictionary<string,System.Collections.Generic.List<InvertedIndexConsoleApp.VerifiedUrl>>' to 'System.Linq.IQueryable<System.Collections.Generic.KeyValuePair<string,System.Collections.Generic.List<InvertedIndexConsoleApp.VerifiedUrl>>>' c:\users\josh\documents\visual studio 2010\Projects\InvertedIndexConsoleApp\InvertedIndexConsoleApp\Program.cs 79 25 InvertedIndexConsoleApp
Error 2 'System.Collections.Generic.Dictionary<string,System.Collections.Generic.List<InvertedIndexConsoleApp.VerifiedUrl>>' does not contain a definition for 'Where' and the best extension method overload 'System.Linq.Queryable.Where<TSource>(System.Linq.IQueryable<TSource>, System.Linq.Expressions.Expression<System.Func<TSource,bool>>)' has some invalid arguments c:\users\josh\documents\visual studio 2010\Projects\InvertedIndexConsoleApp\InvertedIndexConsoleApp\Program.cs 79 25 InvertedIndexConsoleApp