如何动态获取与不同组合集相关的数据

How to get data related to sets of different combinations dynamically

我想获取与不同组合集相关的数据。

EX :

如果我有table:EmployeeLocation

empNum  locationId  extensionId  year

 534      CX1         AX         2015
 534      LM1         RQ         2015
 677      LM1         ST         2015

回复:534,2015

现在我想查询所有在位置 (CX1 AND LM1) 的员工,他们都在 2015。也许我将组合更改为 (CX1 AND LM1 AND LR3)...等等


如何使类似的东西成为动态组合。根据用户输入的一组位置组合。

DECLARE
     locationId VARCHAR(3);
     year INTEGER;

BEGIN
  EXECUTE IMMEDIATE    

    'SELECT empNum FROM EmployeeLocation
     WHERE locationId = CX1 AND locationId = LM1 AND year = 2015';
END;

这可能是描述查询的一种简单方法。我认为剩下的就是一个事件处理程序来指定要使用的locationId

这里更详细:

CREATE OR REPLACE PROCEDURE empNum(locId VARCHAR(3), yr VARCHAR(4)) IS
   TYPE cur_typ IS REF CURSOR;
   c cur_typ;
   locationId VARCHAR(3);
   year INTEGER;
BEGIN
  EXECUTE IMMEDIATE    

    'SELECT empNum FROM EmployeeLocation
     WHERE locationId = locId AND locationId = locId AND year = yr';
END;

我认为使用 cur_type 作为参考游标可以用于查询字符串。

来源:https://docs.oracle.com/cd/A97630_01/appdev.920/a96590/adg09dyn.htm

您可以使用 Linq 的方法如下。

假设你有一个 class 如下:

private class EmployeeLocation {
    public int EmpNum {get; set;} 
    public string LocationId {get; set;}
    public string ExtensionId {get; set;}
    public int Year {get; set;} 
}

您可以创建一个方法来处理基于输入中提供的参数的搜索:

public IEnumerable<EmployeeLocation> Search(
    int[] empNumArray, 
    string[] locationIdArray, 
    string[] extensionIdArray, 
    int[] yearArray){

    IEnumerable<EmployeeLocation> result = EmployeeLocationList;
    if (empNumArray != null){
        foreach(var empNum in empNumArray){
            result = result.Where(r => r.EmpNum == empNum);
        }
    }
    if (locationIdArray != null){
        foreach(var locationId in locationIdArray){
            result = result.Where(r => r.LocationId);
        }
    }
    if (extensionIdArray != null){
        foreach(var extensionId in extensionIdArray){
            result = result.Where(r => r.ExtensionId == extensionId);
        }
    }
    if (yearArray != null){
        foreach(var year in yearArray ){
            result = result.Where(r => r.Year == year);
        }
    }
    return result;
}

检查每个条件然后取结果的交集如何?

    public IEnumerable<int> Search(IEnumerable<Employee> employees, IEnumerable<Condition> conditions) {
        var subresults = new List<IEnumerable<int>>();
        IEnumerable<int> result = null;
        foreach (var condition in conditions) {
            subresults.add(employees.Where(e => e.CheckCondition(condition)).Select(e => e.EmpNum));
        }
        foreach (var subresult in subresults) {
            if (result == null) {
                result = subresult;
            } else {
                result = result.Intersect(subresult)
            }
        }
        return result;
    }