0

In LINQ, is it possible to pass .Where conditions as parameter?

IList<Object> obj = persons
    .Where(p => p.Text.Contains("x") || p.Text.Contains("y"))
    .ToList();

So that more than one dynamic conditions

Rufus L
  • 32,853
  • 5
  • 25
  • 38
Hesoti
  • 57
  • 6
  • 3
    you can define `Func, bool> objectWhere` then pass it `persons.Where(objectWhere);` – iSR5 May 26 '20 at 23:28

1 Answers1

0

The single line you have posted is equivalent to the following:

bool filter( Person p )
{
    return p.Text.Contains( "x" ) || p.Text.Contains( "y" );
}

IList<Object> obj = persons.Where( filter ).ToList();

I hope this answers your question.

Mike Nakis
  • 46,450
  • 8
  • 79
  • 117