簡體   English   中英

PredicateBuilder 的問題

[英]Trouble with PredicateBuilder

我在使用 PredicateBuilder 將“Or Where”子句動態添加到 LINQ 語句時遇到問題。 我將首先解釋我要完成的工作。

我有一個倒排索引,它存儲來自一堆鏈接標題的關鍵字。 我正在使用一個,因此我可以根據這些關鍵字快速搜索這些鏈接。 倒排索引的類型

Dictionary<string, List<VerifiedUrl>>

所以基本上每個單詞都與包含該單詞的 URL 列表相關聯。

我的下一個階段是使倒排索引可搜索。 因此,當我搜索“藍色”時,會返回與“the”或“blue”鍵相關的所有鏈接。 在幾次 Google 搜索之后,將“Or Where”子句動態添加到 LINQ 語句的最佳方法似乎是通過PredicateBuilder class。 我在使用已構建的謂詞的最后一步時遇到問題。

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 上顯示“無法從用法中推斷出 arguments 類型。嘗試准確指定 arguments 類型。”

我嘗試改變:

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

問題 that.Where 參數必須是 Func,但 PredicateBuilder.Or 返回 Expression< Func<..>>。 嘗試這個

test = invertedIndex.Where(predicate.Compile());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM