How to implement search functionality in C#/ASP.NET MVC

前端 未结 7 2030
执笔经年
执笔经年 2021-02-02 01:54

I am developing an ASP.NET MVC 3 application using C# and Razor.

I have a search form that looks like this: \"searc

相关标签:
7条回答
  • 2021-02-02 02:00

    create method and call it on button click demo below

    public List gettaskssdata(int c, int userid, string a, string StartDate, string EndDate, string ProjectID, string statusid) {

            List<tbltask> tbtask = new List<tbltask>();
    
    
            var selectproject = entity.tbluserprojects.Where(x => x.user_id == userid).Select(x => x.Projectid);
    
            if (statusid != "" && ProjectID != "" && a != "" && StartDate != "" && EndDate != "")
            {
                int pid = Convert.ToInt32(ProjectID);
                int sid = Convert.ToInt32(statusid);
                DateTime sdate = Convert.ToDateTime(StartDate).Date;
                DateTime edate = Convert.ToDateTime(EndDate).Date;
                tbtask = entity.tbltasks.Include(x => x.tblproject).Include(x => x.tbUser).Where(x => selectproject.Contains(x.ProjectId) && (x.tblproject.company_id == c) && (x.tblproject.ProjectId == pid) && (x.tblstatu.StatusId == sid) && (x.TaskName.Contains(a) || x.tbUser.User_name.Contains(a)) && (x.StartDate >= sdate && x.EndDate <= edate)).OrderByDescending(x => x.ProjectId).ToList();
            }
            else if (statusid == "" && ProjectID != "" && a != "" && StartDate != "" && EndDate != "")
            {
                int pid = Convert.ToInt32(ProjectID);
                DateTime sdate = Convert.ToDateTime(StartDate).Date;
                DateTime edate = Convert.ToDateTime(EndDate).Date;
                tbtask = entity.tbltasks.Include(x => x.tblproject).Include(x => x.tbUser).Where(x => selectproject.Contains(x.ProjectId) && (x.tblproject.company_id == c) && (x.tblproject.ProjectId == pid) && (x.TaskName.Contains(a) || x.tbUser.User_name.Contains(a)) && (x.StartDate >= sdate && x.EndDate <= edate)).OrderByDescending(x => x.ProjectId).ToList();
            }
            else if (ProjectID == "" && statusid != "" && a != "" && StartDate != "" && EndDate != "")
            {
                int sid = Convert.ToInt32(statusid);
                DateTime sdate = Convert.ToDateTime(StartDate).Date;
                DateTime edate = Convert.ToDateTime(EndDate).Date;
                tbtask = entity.tbltasks.Include(x => x.tblproject).Include(x => x.tbUser).Where(x => selectproject.Contains(x.ProjectId) && (x.tblproject.company_id == c) && (x.tblstatu.StatusId == sid) && (x.TaskName.Contains(a) || x.tbUser.User_name.Contains(a)) && (x.StartDate >= sdate && x.EndDate <= edate)).OrderByDescending(x => x.ProjectId).ToList();
            }
            else if(ProjectID!="" && StartDate == "" && EndDate == "" && statusid == ""  && a == "")
            {
                int pid = Convert.ToInt32(ProjectID);
                tbtask = entity.tbltasks.Include(x => x.tblproject).Include(x => x.tbUser).Where(x => selectproject.Contains(x.ProjectId) && (x.tblproject.company_id == c) && (x.tblproject.ProjectId == pid)).OrderByDescending(x => x.ProjectId).ToList();
    
            }
            else if(statusid!="" && ProjectID=="" && StartDate == "" && EndDate == ""  && a == "")
            {
                int sid = Convert.ToInt32(statusid);
                tbtask = entity.tbltasks.Include(x => x.tblproject).Include(x => x.tbUser).Where(x => selectproject.Contains(x.ProjectId) && (x.tblproject.company_id == c) && (x.tblstatu.StatusId == sid) ).OrderByDescending(x => x.ProjectId).ToList();
            }
            else if (a == "" && StartDate != "" && EndDate != "" && ProjectID != "")
            {
                int pid = Convert.ToInt32(ProjectID);
                DateTime sdate = Convert.ToDateTime(StartDate).Date;
                DateTime edate = Convert.ToDateTime(EndDate).Date;
                tbtask = entity.tbltasks.Include(x => x.tblproject).Include(x => x.tbUser).Where(x => selectproject.Contains(x.ProjectId) && (x.tblproject.ProjectId == pid) && (x.StartDate >= sdate && x.EndDate <= edate)).OrderByDescending(x => x.ProjectId).ToList();
    
            }
            else if (StartDate == "" && EndDate == "" && statusid != "" && ProjectID != "" && a != "")
            {
                int pid = Convert.ToInt32(ProjectID);
                int sid = Convert.ToInt32(statusid);
                tbtask = entity.tbltasks.Include(x => x.tblproject).Include(x => x.tbUser).Where(x => selectproject.Contains(x.ProjectId) && (x.tblproject.company_id == c) && (x.tblproject.ProjectId == pid) && (x.tblstatu.StatusId == sid) && (x.TaskName.Contains(a) || x.tbUser.User_name.Contains(a))).OrderByDescending(x => x.ProjectId).ToList();
            }
            else if (a == "" && StartDate == "" && EndDate == "" && ProjectID != "" && statusid != "")
            {
                int pid = Convert.ToInt32(ProjectID);
                int sid = Convert.ToInt32(statusid);
                tbtask = entity.tbltasks.Include(x => x.tblproject).Include(x => x.tbUser).Include(x => x.tblstatu).Where(x => selectproject.Contains(x.ProjectId) && x.tblproject.company_id == c && x.tblproject.ProjectId == pid && x.tblstatu.StatusId == sid).OrderByDescending(x => x.ProjectId).ToList();
            }
            else if (a != "" && StartDate == "" && EndDate == "" && ProjectID == "" && statusid == "")
            {
                tbtask = entity.tbltasks.Include(x => x.tblproject).Include(x => x.tbUser).Where(x => selectproject.Contains(x.ProjectId) && (x.tblproject.company_id == c) && (x.TaskName.Contains(a) || x.tbUser.User_name.Contains(a))).OrderByDescending(x => x.ProjectId).ToList();
    
            }
            else if (a != "" && ProjectID != "" && StartDate == "" && EndDate == "" && statusid == "")
            {
                int pid = Convert.ToInt32(ProjectID);
                tbtask = entity.tbltasks.Include(x => x.tblproject).Include(x => x.tbUser).Where(x => selectproject.Contains(x.ProjectId) && (x.tblproject.company_id == c) && (x.tblproject.ProjectId == pid) && (x.TaskName.Contains(a) || x.tbUser.User_name.Contains(a))).OrderByDescending(x => x.ProjectId).ToList();
            }
            else if (a != "" && StartDate != "" && EndDate != "" && ProjectID == "" && statusid == "")
            {
                DateTime sdate = Convert.ToDateTime(StartDate).Date;
                DateTime edate = Convert.ToDateTime(EndDate).Date;
                tbtask = entity.tbltasks.Include(x => x.tblproject).Include(x => x.tbUser).Where(x => selectproject.Contains(x.ProjectId) && (x.tblproject.company_id == c) && (x.TaskName.Contains(a) || x.tbUser.User_name.Contains(a)) && (x.StartDate >= sdate && x.EndDate <= edate)).OrderByDescending(x => x.ProjectId).ToList();
            }
            else
            {
                tbtask = entity.tbltasks.Include(x => x.tblproject).Include(x => x.tbUser).Where(x => selectproject.Contains(x.ProjectId) && x.tblproject.company_id == c).OrderByDescending(x => x.ProjectId).ToList();
            }
            return tbtask;
    
        }
    
    0 讨论(0)
  • 2021-02-02 02:01

    Have you looked into using Lucene.NET for this project? given the nature of your searches it would be very simple to build that using Lucene, as it allows you to combine filters on different columns just like your requirements

    0 讨论(0)
  • 2021-02-02 02:06

    Not sure if you are using MS SQL. Seems SQL could do most of the work for you, and you can build dynamic queries. Obviously the select/from statement needs work, but you can get the idea from the where clause.

    DECLARE @SEARCHTYPE VARCHAR(20)
    DECLARE @SEARCHTERM VARCHAR(100)
    
    SELECT
        [FIELDS]
    FROM
        [TABLE]
    WHERE
        (@SEARCHTYPE = 'BEGINSWITH' AND [FIELD] LIKE @SEARCHTERM + '%') OR
        (@SEARCHTYPE = 'ENDSWITH' AND [FIELD] LIKE '%' + @SEARCHTERM) OR
        (@SEARCHTYPE = 'EQUALS' AND [FIELD] = @SEARCHTERM)
    
    0 讨论(0)
  • 2021-02-02 02:16

    You can build expression tree for where predicate using code. For example,

    public static IQueryable<T> DynamicWhere<T>(this IQueryable<T> src, string propertyName, string value)
    {
        var pe = Expression.Parameter(typeof(T), "t");
        var left = Expression.Property(pe, typeof(T).GetProperty(propertyName));
        var right = Expression.Constant(value);
        // Illustrated a equality condition but you can put a switch based on some parameter
        // to have different operators
        var condition = Expression.Equal(left, right);
    
        var predicate = Expression.Lambda<Func<T, bool>>(condition, pe);
        return src.Where(predicate);
    }
    

    Use it as Orders.DynamicWhere(searchBy, searchValue). You can add one more parameter to accept the operator such as Equals, Greater Than etc to complete the function.

    See these links for more info:

    http://msdn.microsoft.com/en-us/library/bb882637.aspx

    http://msdn.microsoft.com/en-us/library/bb397951.aspx

    Also check list of methods on the Expression class to get an idea.

    0 讨论(0)
  • 2021-02-02 02:17

    We started out resolving similar queries against our Entity Framework model using dynamic linq queries. However, our attempts to generalize query generation resulted in bad performance due to EF being confused by the resulting complex expressions, so in the end horrible SQL was produced.

    We resorted to Entity SQL.

    0 讨论(0)
  • 2021-02-02 02:25

    You can use Dynamic Linq and you can create the Where clausole with a utility class like this:

    public class Criteria 
    {
       StringBuilder sb = new StringBuilder();
       bool first = true;
    
       public void And(string property, string dbOperator, string value) {
           if (first)
           {
               sb.Append(" ").Append(property).Append(" ");
               sb.Append(" ").Append(dbOperator).Append(" ");
               sb.Append(" ").Append(value).Append(" ");
               first = false;
           }
           else
           {
               sb.Append(" && ").Append(property).Append(" ");
               sb.Append(" ").Append(dbOperator).Append(" ");
               sb.Append(" ").Append(value).Append(" ");
           }
       }
    
       public void Or(string property, string dbOperator, string value)
       {
           if (first)
           {
               sb.Append(" ").Append(property).Append(" ");
               sb.Append(" ").Append(dbOperator).Append(" ");
               sb.Append(" ").Append(value).Append(" ");
               first = false;
           }
           else
           {
               sb.Append(" || ").Append(property).Append(" ");
               sb.Append(" ").Append(dbOperator).Append(" ");
               sb.Append(" ").Append(value).Append(" ");
           }
       }
    
       public string ToString() 
       {
           return sb.ToString();
       }
    
    }
    

    So you can build a Criteria with many properties using Or or And methods and put it in the Where operator of Dynamic Linq.

    0 讨论(0)
提交回复
热议问题