I'm having trouble using PredicateBuilder to dynamically add "Or where" clauses to a LINQ statement. I will explain what I am trying to accomplish in the first place.
I have an inverted index that stores keywords from link link headers. I use one, so I can quickly find these links based on these keywords. The inverted index is of type
Dictionary<string, List<VerifiedUrl>>
Thus, each word is associated with a list of URLs containing that word.
The next step I'm in is to make the inverted index searchable. Therefore, when I search for βblue,β I return all links associated with the key βeitherβ or βblueβ. After several Google searches, it seemed like the best way to dynamically add "Or where" sentences to a LINQ statement through the PredicateBuilder class . I had problems with the last step when I use the predicate that I created.
var invertedIndex = new Dictionary<string, List<VerifiedUrl>>();
Console.WriteLine("\nEnter words to see if there are matches");
string query = Console.ReadLine();
Console.WriteLine();
string[] words = query.Split(',', ' ');
var predicate = PredicateBuilder.False<KeyValuePair<string, List<VerifiedUrl>>>();
foreach (string w in words)
{
string temp = w;
predicate = predicate.Or(p => p.Key == temp);
}
test = invertedIndex.Where(predicate);
I get an error message. Hover over .Where. "Type arguments cannot be taken out of use. Try specifying type arguments exactly."
I tried to change:
var predicate = PredicateBuilder.False<KeyValuePair<string, List<VerifiedUrl>>>();
to
Expression<Func<KeyValuePair<string, List<VerifiedUrl>>, bool>> predicate = PredicateBuilder.False<KeyValuePair<string, List<VerifiedUrl>>>();
but it had no effect. In the error console, I get different errors:
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