PredicateBuilder 无法转换为 IQueryable?
我有一个函数:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
PredicateBuilder 需要一个
IQueryable
来发挥其魔力。只需更改示例中的最后一行即可使其工作。
PredicateBuilder needs an
IQueryable<T>
to do its magic. Just change the last line in your example toto make it work.
您还可以将表达式编译为 Func;
它适用于 LinqWhere 参数。
Also you can compile expession to Func<T, bool>
It is suitable for Linq Where parameters.