Filter Criteria Search using LINQ

Viewed 2594

I am 'newer' to LINQ queries have another one of those questions where I have something going but not sure if this is the most effective way to go about it. In my project, I am working in a real DB, but for a sake of simplicity, here I will condense it down to a simple list of employees:

var employees = new List<Employee>
        {
            new Employee { Id = 0, firstName = "James", LastName = "Bond", Manager = "M", StartDate = DateTime.Now },
            new Employee { Id = 1, firstName = "Eric", LastName = "Bond", Manager = "M", StartDate = DateTime.Now },
            new Employee { Id = 2, firstName = "Sue", LastName = "Milton", Manager = "Q", StartDate = DateTime.Now },
            new Employee { Id = 3, firstName = "Olivia", LastName = "Milton", Manager = "M", StartDate = DateTime.Now },
            new Employee { Id = 4, firstName = "Alice", LastName = "Raymond", Manager = "M", StartDate = DateTime.Now },
            new Employee { Id = 5, firstName = "James", LastName = "Skywalker", Manager = "M", StartDate = DateTime.Now },
            new Employee { Id = 6, firstName = "Luke", LastName = "Skywalker", Manager = "M", StartDate = DateTime.Now },

        };

I have to search in this list based on given criteria.. where criteria is combination of various fields with OR and AND operations with in the fields for example get me all employees where:

  • firstName = "James" OR "eric" AND manager = "Q"
  • lastname = "bond" OR "Martha"
  • firstName = "James" AND Lastname = "Bond" and so on...

This is going to be a web API call and I have to do this in one method. The other challenge is that each search parameter is 'optional" i.e , they can pass me a list of firstnames and a manager name and ignore the last names parameters etc. So here is what I started coded:

    public IList<Employee> GetFilteredEmployees(IList<String> firstnames = null,
                                                IList<String> lastnames = null,
                                                IList<String> managers = null)
    {
    if (firstnames != null && firstnames.Any())
     {
          foreach (var fn in firstnames)
          {
             employeeByFn = employees.Where(emp => emp.firstName == fn).ToList<Employee>();
          }

      }

     if (lastnames != null && lastnames.Any())
     {
         foreach (var ln in lastnames)
         {
             employeeByLn = employees.Where(emp => emp.LastName == ln).ToList<Employee>();
         }
      }

     ..... // code ellided
    }

As you can see, this is getting ugly even with a few search criteria parameters. In my real project, I have up to 16 of those. Also at the end of all these sub-queries, I have to merge my results into one employee list and return that keeping in mind that any of the sub-query result may be null.

I am sure this is not a unique problem and I see similar questions asked before but not exactly the same problem. What would be elegant way of doing this that is also easy to maintain .i.e if they decide to add more search criteria later (say by start Date), I want to be able to easily modify my method to handle that.

Thanks a bunch for looking.

1 Answers
Related