简体   繁体   English

带有多个 where 子句的 EFCore 查询,它们一起充当 AND 但不充当 OR

[英]EFCore query with multiple where clause that together act as AND but not OR

This is very obvious but tricky question.这是一个很明显但很棘手的问题。 I could not find its answer on web or simply i am missing keywords that could find its answer.我在网上找不到它的答案,或者我只是缺少可以找到答案的关键字。

Let's say we have many conditions based on which we want to filter data.假设我们有许多要过滤数据的条件。 These conditions are in multiple blocks.这些条件在多个块中。 How to write them so that they work as AND clause but not OR providing they participate only in certain condition.如何编写它们以便它们作为AND子句而不是OR工作AND前提是它们仅在特定条件下参与。

    var query = _entities.AsQueryable();

    if (model.CityId != default)
    {
        query = query.Where(x => x.CityId == model.CityId);
    }
    if (!string.IsNullOrWhiteSpace(model.PostalCode))
    {
        query = query.Where(x => x.Proppostcode == model.PostalCode);
    }
    if (!string.IsNullOrWhiteSpace(model.AirportCode))
    {
        query = query.Where(x => x.AirportCode == model.AirportCode);
    }

Let me know guys if this question need more details.如果这个问题需要更多细节,请告诉我。 Thank you!谢谢!

am I missing keywords that could find its answer?我是否缺少可以找到答案的关键字?

Yes, This missing keyword is Dynamic Expression or Dynamic Query .是的,这个缺少的关键字是Dynamic ExpressionDynamic Query If you follow these keywords you'll find a lot of solutions to your problem.如果您遵循这些关键字,您会发现很多问题的解决方案。

As this tutorial said:正如本教程所说:

Dynamic Query allows you to perform dynamic where clause, select , order by , with string expression at runtime . Dynamic Query允许您在运行时使用字符串表达式执行动态where子句、 selectorder by

you can follow below links, too:您也可以点击以下链接:

good luck.祝你好运。

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

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