1


I am working on ASP.net MVC 3.0 Razor View Engine Application.
I have a Advance Search form to get the list of Customers found
Which will have fields like

  • FirstName TextBox
  • LastName TextBox
  • DOB TextBox
  • Address TextBox
  • Gender DropDown
  • Zipcode TextBox
  • Ignore Address checkBox

  • Finally the Submit Button.
    I have a list of Customers in a List object.
    No field is Mandatory in the Search Form.
    When User hits search based on the Values he provided in the Form, my Logic should return the matching results from the List Object that is holding all Customer details.

    So I would like to pick an idea from you.
    to write a best logic that will find the Customer based on user provided info.

    I started with something like this

    if (fname != "" && lname == "")
            {
                return _CustomerList.FindAll(p => p.FirstName == fname);
            }
            else if (fname == "" && lname != "")
            {
                return _CustomerList.FindAll(p => p.LastName == lname);
            }
    
            else if (fname != "" && lname != "")
            {
                return _CustomerList.FindAll(p => p.FirstName == fname && p.LastName == lname);
            }
    return _CustomerList;
    

    I don't think this is an efficient way.

    Any advises would be a great help. thank you for reading. :)

    3
    • 1
      this is soooo old fashion. Can't you have only one text field for input like chrome does? the quick search is modern and used everywhere nowadays, chrome, Win7 Start menu, IE9, latest black berry... you do execute a smart advanced search in background but ask the user to enter search value only in one box. If google can search the whole Internet you should be able to search in your database. Oct 5, 2011 at 17:20
    • @DavidePiras "you should be able to search in your database", I wish I could DV comments. Comments like these show very major flaws in understanding. The database google does text analysis on is extremely different than any standard transactional database. Full text search done well is an invariably hard task to accomplish. Oct 5, 2011 at 17:26
    • that is not what I meant, did not want to compare google BigTable or GFS and indexing with yours. Anyway free to decide. We use one box search in several projects and users are initially lost then they learn it and they love it. you can search by FT or by classic compare/where clause in a selected list of fields. Resulting User Experience is smooth and much appreciated. Oct 5, 2011 at 17:29

    2 Answers 2

    0

    Use Linq instead and an IEnumerable<Customer> - you can compose your query with additional Where() clauses as needed and at the end return the matching items as list using ToList().

    var matchingCustomers = _CustomerList;
    
    if (!string.IsNullOrEmpty(lname))
    {
        matchingCustomers = matchingCustomers.Where(x => x.LastName == lname);
    }
    
    if (!string.IsNullOrEmpty(fname))
    {
        matchingCustomers = matchingCustomers.Where(x => x.FirstName == fname);
    }
    
    //...
    
    return matchingCustomers.ToList();
    
    1
    • I think this will work for me. I will post back once i implement this fully
      – HaBo
      Oct 5, 2011 at 17:41
    0

    I think this will help you..

    if (!string.IsNullOrEmpty(lname) || ! string.IsNullOrEmpty(fname))
    
    {
    
    _CustomerList= _CustomerList.Where(x =>x.LastName.Contains(lname.ToUpper().Trim()) ||               
     x.FirstName.Contains(fname.ToUpper().Trim())
    );
    
    return _CustomerList.ToList();
    
    }
    

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

    Not the answer you're looking for? Browse other questions tagged or ask your own question.