Luke101
Luke101

Reputation: 65308

How to perform a dynamic query with Linq

I am trying to perform a dynamic OR. For example:

test = test.Where(z => z.Id > 1);
test = test.Where(x => x.Name == "Admin"); //or name equals admin

I am going to pass the first query through a method then need to perform and OR instead of an and. How do I do this with Linq?

Upvotes: 0

Views: 86

Answers (2)

Tim S.
Tim S.

Reputation: 56586

Try this:

test = test.Where(z => z.Id > 1 || z.Name == "Admin");

Upvotes: 1

Tilak
Tilak

Reputation: 30728

You can use union for OR effect.

    test1 = test.Where(z => z.Id > 1);
    test2 = test.Where(x => x.Name == "Admin"); //or name equals admin

    test = test1.Union(test2)

Upvotes: 1

Related Questions