none
EF: Where is not working RRS feed

  • Question

  • I have a simple page where i am showing data in tabular format. i have one search textbox and i want whatever user will write in search textbox that will be filter the data coming from DB. i am using EF and also using PagedList helper in mvc page to show the pagination link.

    this area of coding is not working

        if (!String.IsNullOrEmpty(SearchText))
        {
            customer = db.Customers.Where(s => s.CompanyName.ToUpper().Contains(SearchText.ToUpper())
            || s.ContactName.ToUpper().Contains(SearchText.ToUpper())
            || s.ContactTitle.ToUpper().Contains(SearchText.ToUpper())
            || s.Address.ToUpper().Contains(SearchText.ToUpper()));
        }

    here giving full action code for better understand the flow.

    public ActionResult Index(int? page, string SortColumn, string CurrentSort, String SearchText)
            {
                var customer = (object) null; 
    
                int pageSize = 5;
                int pageNumber = (page ?? 1);
                ViewBag.CurrentPage = pageNumber;
                SortColumn = String.IsNullOrEmpty(SortColumn) ? "CompanyName" : SortColumn;
                ViewBag.CurrentSort = SortColumn;
                ViewBag.OldSort = CurrentSort;
                ViewBag.SearchText = SearchText;
    
                if (!String.IsNullOrEmpty(SearchText))
                {
                    customer = db.Customers.Where(s => s.CompanyName.ToUpper().Contains(SearchText.ToUpper())
                    || s.ContactName.ToUpper().Contains(SearchText.ToUpper())
                    || s.ContactTitle.ToUpper().Contains(SearchText.ToUpper())
                    || s.Address.ToUpper().Contains(SearchText.ToUpper()));
                }
    
                switch (SortColumn)
                {
                    case "CompanyName":
                        if (SortColumn.Equals(CurrentSort))
                        {
                            customer = db.Customers.OrderByDescending(m => m.CompanyName).ToPagedList(pageNumber, pageSize);
                            ViewBag.CurrentSort = "";
                        }
                        else
                            customer = db.Customers.OrderBy(m => m.CompanyName).ToPagedList(pageNumber, pageSize);
                        break;
    
                    case "ContactName":
                        if (SortColumn.Equals(CurrentSort))
                        {
                            customer = db.Customers.OrderByDescending(m => m.ContactName).ToPagedList(pageNumber, pageSize);
                            ViewBag.CurrentSort = "";
                        }
                        else
                            customer = db.Customers.OrderBy(m => m.ContactName).ToPagedList(pageNumber, pageSize);
                        break;
    
                    case "ContactTitle":
                        if (SortColumn.Equals(CurrentSort))
                        {
                            customer = db.Customers.OrderByDescending(m => m.ContactTitle).ToPagedList(pageNumber, pageSize);
                            ViewBag.CurrentSort = "";
                        }
                        else
                            customer = db.Customers.OrderBy(m => m.ContactTitle).ToPagedList(pageNumber, pageSize);
                        break;
    
                    case "Address":
                        if (SortColumn.Equals(CurrentSort))
                        {
                            customer = db.Customers.OrderByDescending(m => m.Address).ToPagedList(pageNumber, pageSize);
                            ViewBag.CurrentSort = "";
                        }
                        else
                            customer = db.Customers.OrderBy(m => m.Address).ToPagedList(pageNumber, pageSize);
                        break;
    
                    case "Default":
                        customer = db.Customers.OrderBy(m => m.CompanyName).ToPagedList(pageNumber, pageSize);
                        break;
                }
    
                return View(customer);
    
                //return View(db.Customers.ToList());
            }

    please tell me where i made the mistake and tell me what to change as a result filter and order should work.

    Friday, February 2, 2018 1:40 PM

Answers

  • Hi Sudip_inn,

    According to your description and related, I create a simple demo with entity framework 6.1.3, which works well, please check if you have related records in your database, does it throw any exception?

    In addition, please remove ToUpper method and check if it works for you.

    if (!String.IsNullOrEmpty(SearchText))
                {
                    customer = db.Customers.Where(s => s.CompanyName().Contains(SearchText)
                    || s.ContactName.Contains(SearchText)
                    || s.ContactTitle.Contains(SearchText)
                    || s.Address().Contains(SearchText));
                }

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Thursday, February 8, 2018 12:43 PM
    Monday, February 5, 2018 2:23 AM
    Moderator

All replies