PredicateBuilder 无法转换为 IQueryable?

发布于 2024-12-31 22:45:28 字数 2404 浏览 2 评论 0原文

我有一个函数:

private IEnumerable<Promotion> MatchesKeyword(IEnumerable<Promotion> list, String keyword)
{
   ...snip...
}

它现在执行 LINQ 查询:

private IEnumerable<Promotion> MatchesKeyword(IEnumerable<Promotion> list, String keyword)
{
   return list.Where(item => item.Name.ContainsText(keyword)
      || item.Description.ContainsText(keyword)
      ...snip...
   );
}

此代码运行良好。

但是我需要将其转换以使用< a href="http://www.albahari.com/nutshell/predicatebuilder.aspx" rel="nofollow noreferrer">PredicateBuilder

private IEnumerable<Promotion> MatchesKeyword(IEnumerable<Promotion> list, String keyword)
{
   var predicate = PredicateBuilder.False<Promotion>();     

   predicate = predicate.Or(item => item.Name.ContainsText(keyword)
         || item.Description.ContainsText(keyword)
         ...snip...
   );

   return list.Where(predicate);
}

奇怪的是,它无法编译。失败的行是:

return list.Where(predicate);

您可以选择错误:

  • 实例参数:无法从“System.Collections.Generic.IEnumerable”转换为“System.Linq.IQueryable”
  • “System.Collections.Generic.IEnumerable”不包含'Where' 的定义和最佳扩展方法重载 'System.Linq.Enumerable.Where(System.Collections.Generic.IEnumerable, System.Func)' 有一些无效参数
  • 参数 2:无法从 'System.Linq.Expressions.Expression>' 转换到“System.Func”

有什么问题? IEnumerable 进去,IEnumerable 出来。

老实说,我阅读了 PredicateBuilder 上的页面 我不明白任何


关于我需要更改为 PredicateBuilder提示是:

private IEnumerable<Promotion> MatchesKeyword(IEnumerable<Promotion> list, String keyword)
{
   var predicate = PredicateBuilder.False<Promotion>();     

   predicate = predicate.Or(item => item.Name.ContainsText(keyword)
         || item.Description.ContainsText(keyword)
         ...snip...
   );

   DateTime dt = TryStrToDate(keyword);
   if (dt)
       predicate = predicate.Or(item => item.PromotionDate == dt);

   return list.Where(predicate);
}

...并不是说我需要理由、问题、实际示例或研究努力提出问题。

i have a function:

private IEnumerable<Promotion> MatchesKeyword(IEnumerable<Promotion> list, String keyword)
{
   ...snip...
}

which right now performs a LINQ query:

private IEnumerable<Promotion> MatchesKeyword(IEnumerable<Promotion> list, String keyword)
{
   return list.Where(item => item.Name.ContainsText(keyword)
      || item.Description.ContainsText(keyword)
      ...snip...
   );
}

This code works well enough.

But i need to convert it to use a PredicateBuilder:

private IEnumerable<Promotion> MatchesKeyword(IEnumerable<Promotion> list, String keyword)
{
   var predicate = PredicateBuilder.False<Promotion>();     

   predicate = predicate.Or(item => item.Name.ContainsText(keyword)
         || item.Description.ContainsText(keyword)
         ...snip...
   );

   return list.Where(predicate);
}

Which, strangely, doesn't compile. The failing line is:

return list.Where(predicate);

You can take your pick of the errors:

  • Instance argument: cannot convert from 'System.Collections.Generic.IEnumerable' to 'System.Linq.IQueryable'
  • 'System.Collections.Generic.IEnumerable' does not contain a definition for 'Where' and the best extension method overload 'System.Linq.Enumerable.Where(System.Collections.Generic.IEnumerable, System.Func)' has some invalid arguments
  • Argument 2: cannot convert from 'System.Linq.Expressions.Expression>' to 'System.Func'

What's the problem? IEnumerable goes in, IEnumerable comes out.

i'll be honest, i read the page on PredicateBuilder and i don't understand any of it.


A hint of why i need to change to PredicateBuilder is:

private IEnumerable<Promotion> MatchesKeyword(IEnumerable<Promotion> list, String keyword)
{
   var predicate = PredicateBuilder.False<Promotion>();     

   predicate = predicate.Or(item => item.Name.ContainsText(keyword)
         || item.Description.ContainsText(keyword)
         ...snip...
   );

   DateTime dt = TryStrToDate(keyword);
   if (dt)
       predicate = predicate.Or(item => item.PromotionDate == dt);

   return list.Where(predicate);
}

...not that i need a reason, problem, practical example, or research effort to ask a question.

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

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

发布评论

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

评论(2

抚你发端 2025-01-07 22:45:28

PredicateBuilder 需要一个 IQueryable 来发挥其魔力。只需更改示例中的最后一行即可

return list.AsQueryable().Where(predicate);

使其工作。

PredicateBuilder needs an IQueryable<T> to do its magic. Just change the last line in your example to

return list.AsQueryable().Where(predicate);

to make it work.

山田美奈子 2025-01-07 22:45:28

您还可以将表达式编译为 Func;

expression.Compile();

它适用于 LinqWhere 参数。

Also you can compile expession to Func<T, bool>

expression.Compile();

It is suitable for Linq Where parameters.

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