PredicateBuilder 的问题

发布于 2024-11-09 15:05:07 字数 2877 浏览 3 评论 0原文

我在使用 PredicateBuilder 向 LINQ 语句动态添加“OrWhere”子句时遇到问题。我将首先解释我想要完成的任务。

我有一个倒排索引,用于存储一堆链接标题中的关键字。我正在使用一个,这样我就可以根据这些关键字快速搜索这些链接。倒排索引的类型为

Dictionary<string, List<VerifiedUrl>>

So ,基本上每个单词都与包含该单词的 URL 列表相关联。

我的下一阶段是使倒排索引可搜索。因此,当我搜索“the blue”时,我会返回与键“the”或“blue”相关的所有链接。经过几次 Google 搜索后,似乎将“OrWhere”子句动态添加到 LINQ 语句的最佳方法是通过 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

I'm having trouble using PredicateBuilder to dynamically add "Or Where" clauses to a LINQ statement. I'll explain what I'm trying to accomplished first.

I have an inverted index that stores keywords from the titles of a bunch of links. I'm using one so I can quickly search through these links based on these keywords. The inverted index is of type

Dictionary<string, List<VerifiedUrl>>

So basically every word is associated to a list of URLs containing that word.

The next stage I'm at is making the inverted index searchable. So when I search for "the blue", I'm returned all the links associated with the key "the" or "blue". After a few Google searches, it seemed like the best way to dynamically add "Or Where" clauses to a LINQ statement was through the PredicateBuilder class. I'm having trouble with the last step where I use the predicate that I've built.

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);

I get an error on the .Where statement. Hovering over the .Where displays "The type arguments cannot be inferred from the usage. Try specifying the type arguments exactly."

I tried changing:

var predicate = PredicateBuilder.False<KeyValuePair<string, List<VerifiedUrl>>>();

to

Expression<Func<KeyValuePair<string, List<VerifiedUrl>>, bool>> predicate = PredicateBuilder.False<KeyValuePair<string, List<VerifiedUrl>>>();

but this had no effect. In the error console I actually 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风渺 2024-11-16 15:05:07

.Where 参数必须是 Func,但 PredicateBuilder.Or 返回 Expression< 的问题函数<...>>。
试试这个

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

the problem that .Where argument must be a Func, but PredicateBuilder.Or returns Expression< Func<..>>.
Try this

test = invertedIndex.Where(predicate.Compile());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文