【问题标题】:Trouble with PredicateBuilderPredicateBuilder 的问题
【发布时间】:2011-05-24 04:37:28
【问题描述】:

我在使用 PredicateBuilder 将“Or Where”子句动态添加到 LINQ 语句时遇到问题。我会先解释我要完成的工作。

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

Dictionary<string, List<VerifiedUrl>>

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

我的下一个阶段是使倒排索引可搜索。因此,当我搜索“蓝色”时,会返回与“the”或“blue”键相关的所有链接。经过几次 Google 搜索后,将“Or Where”子句动态添加到 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

【问题讨论】:

  • 尝试使用invertedIndex.AsQueryable().Where(predicate)
  • @Eranga - 宾果游戏!谢谢。 Albahari 的示例是在 DataContext 的上下文中,不清楚是否需要将本地、非 db 集合强制转换为 IQueryable 才能直接接受 Expressions。
  • .AsQueryable() 修复了我在尝试查询 ICollection 时遇到的类似问题(错误 2)(使用 .AsQueryable() 而不是 .ToList())。这尤其令人困惑,因为 .ToList() 还允许您访问 .Where() 方法,但它不会接受谓词作为参数。
  • 如果您使用委托和 lambdas 而不是表达式树,我相信您可以在 LINQ to Objects 上使用 PredicateBuilder。见:stackoverflow.com/questions/7094930/…

标签: c# linq where predicatebuilder


【解决方案1】:

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

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

【讨论】:

  • 非常感谢,这正是我所需要的。
猜你喜欢
  • 1970-01-01
  • 2013-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-23
  • 1970-01-01
相关资源
最近更新 更多