仅从 class 个属性中获取过滤后的元素

Only get filtered elements from class properties

下面是代码的样子。请注意,所有 EmployeeSomethingA、EmployeeSomethingB、EmployeeSomethingC class 都有一个共同的 EmployeeID 属性.

class EmployeesData
    {
        public List<EmployeeSomethingA> EmployeeSomethingAs { get; set; }
        public List<EmployeeSomethingB> EmployeeSomethingBs { get; set; }
        public List<EmployeeSomethingC> EmployeeSomethingCs { get; set; }
    }

    class EmployeeSomethingA
    {
        public int EmployeeID { get; set; }
        public string SomethingA { get; set; }
    }

    class EmployeeSomethingB
    {
        public int EmployeeID { get; set; }
        public string SomethingB { get; set; }
        public float SomethingBA { get; set; }
    }
    class EmployeeSomethingC
    {
        public int EmployeeID { get; set; }
        public string SomethingC { get; set; }
        public Guid SomethingCA { get; set; }
        public double SomethingCB { get; set; }
    }

我想在 EmployeesData class 上添加功能,以便我可以为特定的 EmployeeID 从 EmployeeSomethingAs、EmployeeSomethingBs 和 EmployeeSomethingCs 中检索筛选。我不确定我可以在这种情况下使用 linq,但我想要的是如下内容。

EmployeeDataInstance.Where ( x => x.EmployeeID > 2);

这应该 return 只有一个 EmployeeData 实例,其中 EmployeeSomethingAs、EmployeeSomethingBs 和 EmployeeSomethingCs 将包含 EmployeeID > 2 的数据。我不知道实施 IEnumerable 是否能让我获得这种能力。知道如何实现吗?

你可以尝试类似的东西;

创建包含共享 属性 EmployeeIDEmployee class 和其他 class 派生自它。

class Employee
{
    public int EmployeeID { get; set; }
}
class EmployeeSomethingA : Employee
{
    public string SomethingA { get; set; }
}

class EmployeeSomethingB : Employee
{
    public string SomethingB { get; set; }
}
class EmployeeSomethingC : Employee
{
    public string SomethingC { get; set; }
}

并创建 Where 方法来执行 EmployeesData class;

的 where 子句
class EmployeesData
{
    public List<EmployeeSomethingA> EmployeeSomethingAs { get; set; }
    public List<EmployeeSomethingB> EmployeeSomethingBs { get; set; }
    public List<EmployeeSomethingC> EmployeeSomethingCs { get; set; }

    public void Where(Func<Employee, bool> predicate)
    {
        EmployeeSomethingAs = EmployeeSomethingAs.Where((Func<EmployeeSomethingA, bool>)predicate).ToList();
        EmployeeSomethingBs = EmployeeSomethingBs.Where((Func<EmployeeSomethingB, bool>)predicate).ToList();
        EmployeeSomethingCs = EmployeeSomethingCs.Where((Func<EmployeeSomethingC, bool>)predicate).ToList();
    }
}

用法;

employeesData.Where(x => x.EmployeeID == 1);

编辑

如果您想将列表属性过滤为新的 EmployeesData 实例,您可以像这样修改 Where 方法;

public EmployeesData Where(Func<Employee, bool> predicate)
{
    return new EmployeesData
    {
        EmployeeSomethingAs = EmployeeSomethingAs.Where((Func<EmployeeSomethingA, bool>)predicate).ToList(),
        EmployeeSomethingBs = EmployeeSomethingBs.Where((Func<EmployeeSomethingB, bool>)predicate).ToList(),
        EmployeeSomethingCs = EmployeeSomethingCs.Where((Func<EmployeeSomethingC, bool>)predicate).ToList()
    };
}

var newEmployeesData = employeesData.Where(x => x.EmployeeID == 1);

如果您使用基数 class 并且在使用结果时不关心类型,则可以不用使用 Linq 和单个谓词。我正在为此提供解决方案以及您关心类型的情况:

public class EmployeesData
{
    public List<EmployeeSomethingA> EmployeeSomethingAs { get; private set; }
    public List<EmployeeSomethingB> EmployeeSomethingBs { get; private set; }
    public List<EmployeeSomethingC> EmployeeSomethingCs { get; private set; }

    //Single predicate alternative. 
    public IEnumerable<EmployeeBase> Where(Func<EmployeeBase, bool> selector)
    {
        return EmployeeSomethingAs.Where(selector)
            .Union(EmployeeSomethingBs.Where(selector))
            .Union(EmployeeSomethingCs.Where(selector));
    }

    //Typed alternative
    public SearchResult FindById(int id)
    {
        var a = EmployeeSomethingAs.Where(e => e.EmployeeID == id);
        var b = EmployeeSomethingBs.Where(e => e.EmployeeID == id);
        var c = EmployeeSomethingCs.Where(e => e.EmployeeID == id);

        return new SearchResult(a, b, c);
    }
}

public class SearchResult
{
    public SearchResult(IEnumerable<EmployeeSomethingA> a, IEnumerable<EmployeeSomethingB> b, IEnumerable<EmployeeSomethingC> c)
    {
        As = a;
        Bs = b;
        Cs = c;
    }
    public IEnumerable<EmployeeSomethingA> As { get; private set; }
    public IEnumerable<EmployeeSomethingB> Bs { get; private set; }
    public IEnumerable<EmployeeSomethingC> Cs { get; private set; }
}

public class EmployeeSomethingA : EmployeeBase
{

    public string SomethingA { get; set; }
}

public class EmployeeSomethingB : EmployeeBase
{

    public string SomethingB { get; set; }
}
public class EmployeeSomethingC : EmployeeBase
{        
    public string SomethingC { get; set; }
}

public class EmployeeBase
{
    public int EmployeeID { get; set; }
}

用法差异:

var data = new EmployeesData();

//This will get me an IEnumerable of EmployeeBase
var results = data.Where(e => e.EmployeeID == 5);

//This will get me a SearchResult object with typed results
var results2 = data.FindById(5);