简体   繁体   中英

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?

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)

尝试这个:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM