实体框架核心,其中foreach变量内进行查询

时间:2020-02-15 12:43:44

标签: sql sql-server linq-to-sql ef-core-2.2

我需要下面查询的LINQ版本。

SELECT *
FROM Persons
WHERE (
        Company = '9390'
        AND Costcentre IN ('939140')
        )
    OR (
        Company = '9391'
        AND Costcentre NOT IN (
            '939344'
            ,'939346'
            ,'939347'
            ,'939348'
            )
        )
    OR (Company = '15B9')

我已经用LINQ编写了查询。但是查询中的参数必须来自foreach循环。公司和CostCentre数据来自List<DataPolicy>

DataPolicy {
  public string CompanyCode { get; set; }
  public List<string> IncludedCosts {get;set;} // for where contains
  public List<string> ExcludedCosts {get;set;} // for where not contains
}

我手动写了LINQ查询。

personQuery = personQuery.Where(person => (
              (person.Company == "9390" && person.CostCentre.Contains("939140")) ||
              (person.Company == "9391" && !ids.Contains(person.CostCentre)) ||
                       (person.Company == "15B9")
          ));

我需要使用DataPolicy循环动态创建此查询。我该怎么办?

0 个答案:

没有答案