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:
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With