I have been trying to implement a search page for the admin of my system. Long story short, there a 3 paramaters as criterias. 1. users, 2.projects, 3. customers. The admin can make any combination of those three criterias. for example “I would like to see all users which are assigned to this projects and also all the customers” or “I want to see this customer and this project but all the users” and so on. How do you implement such filtering? I am using asp.net 4.0 and linq just in case.
Here is the function content and it is all. I have made it by if conditions but it is not at all healthy.
public static List<Data.CurrentActivity> GetUsersActivitySearch(string employeeId, string projectId, string customerId, DateTime startDate, DateTime endDate)
{
DataClassesDataContext dc = new DataClassesDataContext(General.ConnectionString);
if(projectId=="" && customerId!="")
return dc.CurrentActivities.Where(t => t.User.RecId.ToString() == employeeId && t.Customer.RecId.ToString() == customerId && t.ActivityDate >= startDate && t.ActivityDate <= endDate).ToList();
else if (projectId != "" && customerId == "")
return dc.CurrentActivities.Where(t => t.User.RecId.ToString() == employeeId && t.Project.RecId.ToString() == projectId && t.ActivityDate >= startDate && t.ActivityDate <= endDate).ToList();
else if (projectId != "" && customerId != "")
return dc.CurrentActivities.Where(t=>t.User.RecId.ToString()==employeeId && t.Customer.RecId.ToString()==customerId && t.Project.RecId.ToString()==projectId && t.ActivityDate>=startDate && t.ActivityDate<=endDate).ToList();
return dc.CurrentActivities.Where(t => t.User.RecId.ToString() == employeeId && t.ActivityDate >= startDate && t.ActivityDate <= endDate).ToList();
}
Now that I see your code, i have a easy solution. You can actually compound where clause on the IQueryable interface because of the lazy execution:
If you had a generalized object that could represent enough information to display your results. Then you could do something like this: