【发布时间】:2012-01-11 22:34:37
【问题描述】:
我一直在尝试为我的系统管理员实现一个搜索页面。长话短说,有 3 个参数作为标准。 1.用户,2.项目,3.客户。管理员可以对这三个标准进行任意组合。例如“我想查看分配给该项目的所有用户以及所有客户”或“我想查看该客户和该项目但所有用户”等等。你如何实现这样的过滤?我正在使用 asp.net 4.0 和 linq 以防万一。
这里是功能内容,仅此而已。我已经通过 if 条件做到了,但它一点也不健康。
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();
}
【问题讨论】:
-
你必须至少试一试,并在你卡住的地方发布一些代码。不要指望别人会想出一个完整的解决方案。
标签: asp.net linq search filtering