简体   繁体   English

在 Entity Framework Core 中使用动态过滤器

[英]Using dynamic filters with Entity Framework Core

I'm developing an application (.Net Core 3.1, C# 8) that is using Entity Framework Core.我正在开发一个使用 Entity Framework Core 的应用程序(.Net Core 3.1、C# 8)。

I would like to filter a table with several filtering options.我想用几个过滤选项过滤一个表。

I'm getting the filter conditions in JSON and I'm deserializing it into an object.我在 JSON 中获取过滤条件,并将其反序列化为 object。 I want to write a where LINQ query which will filter the table based on those dynamic filtering options.我想写一个 where LINQ 查询,它将根据这些动态过滤选项过滤表。

The twist is I need to manage the filtering with many options and combinations.扭曲是我需要使用许多选项和组合来管理过滤。

  • You can filter for the market , country , vendor and the rest of the filter options will be null .您可以过滤marketcountryvendor和 rest 的过滤器选项将是null
  • You would like to filter for country and vendor then the market will be null and also the rest of the filter options.您想过滤countryvendor ,那么market将是null以及过滤器选项的 rest。

I'm querying a huge table, so it is important to write a query which translates fully into SQL.我正在查询一个巨大的表,因此编写一个完全转换为 SQL 的查询很重要。

The following code is not working properly.以下代码无法正常工作。 I'm looking for something similar that can solve this issue:我正在寻找可以解决此问题的类似方法:

var filters = new demoFilterEntity()
{
      Market = new List<string>() { "LAT", "NAM" }
};

var filteredData = demoMainRepository.GetAll().Where(x =>
      x.Market != null && (filters.Market != null ? filters.Market.Contains(x.Market) : false) &&
      x.Country != null && (filters.Country != null ? filters.Country.Contains(x.Market) : false)).ToList();

I would appreciate suggestions on how I can get through this and manage the filtering dynamically.我将不胜感激有关如何解决此问题并动态管理过滤的建议。

If you have only AND conditions you can do it with just chaining Where clauses:如果您只有AND条件,则只需链接Where子句即可:

var query = demoMainRepository.GetAll().Where(x => x.Market != null); 
if(filters.Market != null)
{
    query = query.Where(x => filters.Market.Contains(x.Market));
}
...
var filteredData = query.ToList();

Also as @Lajos Arpad said maybe you need to consider combining fields null checks (ie x.Market != null ) with the filters checks:正如@Lajos Arpad所说,也许您需要考虑将字段 null 检查(即x.Market != null )与过滤器检查相结合:

var query = demoMainRepository.GetAll(); 
if(filters.Market != null)
{
    query = query.Where(x =>  x.Market != null && filters.Market.Contains(x.Market));
}
...
var filteredData = query.ToList();

I'm currently researching this topic and have found dynamic lambda expressions is a thing The .Where method accepts Expression<Func<Entity, bool>> type which you can create dynamically.我目前正在研究这个主题,发现dynamic lambda expressions是一件事.Where方法接受可以动态创建的Expression<Func<Entity, bool>>类型。

Kind of cumbersome but it works good once you wrap your head around it有点麻烦,但是一旦你把头绕在它周围,它就会很好用

I recommend this article in Codemag Dynamic Lambda Expressions我在 Codemag 动态 Lambda 表达式中推荐这篇文章

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

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