I need help finding the most efficient way to merge two in-memory collections with LINQ-to-Objects. In my case, it’s not a simple join or concat because certain items need to be excluded based on a property value.
Let’s use the following example (not very realistic, but it illustrates what I need):
public abstract class Person
{
public String Name { get; set; }
}
public class Employee : Person
{
public DateTime? TerminationDate { get; set; }
}
public class Customer : Person
{
...
}
Note, an Employee may also be a Customer.
I have a list of Employees and a list of Customers and want to generate a list of all Persons following these rules:
- All Customers that are not also Employees
- All Employees that are not also Customer AND are not terminated (TerminationDate == null)
- All Persons that are both Customers and Employees AND are not terminated (TerminationDate == null)
What’s the most efficient way to accomplish this?
While not necessarily incorrect, James’ example is a bit more complicated that I was hoping for and is incomplete as I need a single list as the end result.
Not sure this is the best solution, either, but here’s what I came up with so far: