【问题标题】:How to implement filtering search for admin如何为管理员实现过滤搜索
【发布时间】: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


【解决方案1】:

现在我看到了您的代码,我有一个简单的解决方案。由于延迟执行,您实际上可以在 IQueryable 接口上复合 where 子句:

        DataClassesDataContext dc = new DataClassesDataContext(General.ConnectionString);
        var result = dc.CurrentActivies.Where(t.ActivityDate >= startDate && t.ActivityDate <= endDate);

        if(userId.Length > 0)
            result = result.Where(t => t.User.RecId.ToString() == employeeId);

        if (projectId.Length > 0)
            result = result.Where(t => t.Project.RecId.ToString() == projectId);

        if (customerId.Length > 0)
            result = result.Where(t=>t.Customer.RecId.ToString()==customerId);

        return result.ToList();

如果您有一个可以代表足够信息来显示结果的通用对象。然后你可以这样做:

public IList<SearchResults> Search(string user, string customer, string project)
{
    return (from user in context.Users
        where SqlMethods.Like(user.Name, user) && user.Length > 0
        select new SearchResult
        {
            ... properties you care about
        })
        .Concat(from project in context.Projects
             where SqlMethods.Like(project, project.Name) && project.Length > 0
             select new SearchResult
             {
                 ///
             })
        .Concat(from customer in context.Customers
             where SqlMethods.Like(customer, customer.Name) && customer.Length > 0
             select new SearchResult
             {
                 ///
             })
        .ToList();
}

【讨论】:

  • 我收到此错误:无法翻译表达式'Table(Customer).Where(customer => Like(customer.RecId.ToString(), Invoke(value(System.Func1[System.String])))).Select(customer =&gt; Invoke(value(System.Func1[ Data.CurrentActivity])))' 到 SQL 中,无法将其视为本地表达式。
  • 但我试图在表的主键中搜索没有 user.name。我改用 user.recId
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-12
  • 1970-01-01
  • 2017-04-06
  • 1970-01-01
  • 2017-04-29
相关资源
最近更新 更多