简体   繁体   English

如何使用Linq执行动态查询

[英]How to perform a dynamic query with Linq

I am trying to perform a dynamic OR. 我正在尝试执行动态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. 我将通过一个方法传递第一个查询然后需要执行和OR而不是和。 How do I do this with Linq? 我如何使用Linq做到这一点?

You can use union for OR effect. 你可以使用union来实现OR效果。

    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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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