none
EntityFramework: Can we minimize sorting code RRS feed

  • Question

  • i am fetching data from db using EF. when i am fetching then i am filtering and sorting data too.this way i am sorting by column name. huge line i have to write which looks not good.

    columns type could be string, datetime or double etc.

        switch (SortColumn)
        {
            case "CompanyName":
                ViewBag.OldSort = "CompanyName";
                if (SortColumn.Equals(CurrentSort))
                {
                    customer = customer.OrderByDescending(m => m.CompanyName);
                    ViewBag.CurrentSort = "";
                    ViewBag.SortOrder = "desc";
                }
                else
                {
                    customer = customer.OrderBy(m => m.CompanyName);
                    ViewBag.SortOrder = "asc";
                }
                break;
    
            case "ContactName":
                ViewBag.OldSort = "ContactName";
                if (SortColumn.Equals(CurrentSort))
                {
                    customer = customer.OrderByDescending(m => m.ContactName);
                    ViewBag.CurrentSort = "";
                    ViewBag.SortOrder = "desc";
                }
                else
                {
                    customer = customer.OrderBy(m => m.ContactName);
                    ViewBag.SortOrder = "asc";
                }
                break;
    
            case "ContactTitle":
                ViewBag.OldSort = "ContactTitle";
                if (SortColumn.Equals(CurrentSort))
                {
                    customer = customer.OrderByDescending(m => m.ContactTitle);
                    ViewBag.CurrentSort = "";
                    ViewBag.SortOrder = "desc";
                }
                else
                {
                    customer = customer.OrderBy(m => m.ContactTitle);
                    ViewBag.SortOrder = "asc";
                }
                break;
    
            case "Address":
                ViewBag.OldSort = "Address";
                if (SortColumn.Equals(CurrentSort))
                {
                    customer = customer.OrderByDescending(m => m.Address);
                    ViewBag.CurrentSort = "";
                    ViewBag.SortOrder = "desc";
                }
                else
                {
                    customer = customer.OrderBy(m => m.Address);
                    ViewBag.SortOrder = "asc";
                }
                break;
    
            case "Default":
                ViewBag.OldSort = "CompanyName";
                customer = customer.OrderBy(m => m.CompanyName);
                ViewBag.SortOrder = "asc";
                break;
        }

    please see for 4 columns i have to write so many line of code. suppose if i need to work with 40 columns then how many more line i need to write. so tell me best option to minimize the code for sorting when working with EF.

    EDIT

    see my full working code where full code given to see what approach i take to sort and fetch data with filter. now tell me how could i minimize the code for sorting, filtering with fetching data.

    private CustomersEntities db = new CustomersEntities();
            public int recordcount = 0;
    
            // GET: Customers
            public ActionResult Index(int? page, string SortColumn, string CurrentSort, String SearchText)
            {
                var customer = (from s in db.Customers
                                // select s;
                                select new CustomerDTO
                                {
                                    CustomerID = s.CustomerID,
                                    CompanyName = s.CompanyName,
                                    ContactName = s.ContactName,
                                    ContactTitle = s.ContactTitle,
                                    Address = s.Address
                                });
    
                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 = customer.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":
                        ViewBag.OldSort = "CompanyName";
                        if (SortColumn.Equals(CurrentSort))
                        {
                            customer = customer.OrderByDescending(m => m.CompanyName);
                            ViewBag.CurrentSort = "";
                            ViewBag.SortOrder = "desc";
                        }
                        else
                        {
                            customer = customer.OrderBy(m => m.CompanyName);
                            ViewBag.SortOrder = "asc";
                        }
                        break;
    
                    case "ContactName":
                        ViewBag.OldSort = "ContactName";
                        if (SortColumn.Equals(CurrentSort))
                        {
                            customer = customer.OrderByDescending(m => m.ContactName);
                            ViewBag.CurrentSort = "";
                            ViewBag.SortOrder = "desc";
                        }
                        else
                        {
                            customer = customer.OrderBy(m => m.ContactName);
                            ViewBag.SortOrder = "asc";
                        }
                        break;
    
                    case "ContactTitle":
                        ViewBag.OldSort = "ContactTitle";
                        if (SortColumn.Equals(CurrentSort))
                        {
                            customer = customer.OrderByDescending(m => m.ContactTitle);
                            ViewBag.CurrentSort = "";
                            ViewBag.SortOrder = "desc";
                        }
                        else
                        {
                            customer = customer.OrderBy(m => m.ContactTitle);
                            ViewBag.SortOrder = "asc";
                        }
                        break;
    
                    case "Address":
                        ViewBag.OldSort = "Address";
                        if (SortColumn.Equals(CurrentSort))
                        {
                            customer = customer.OrderByDescending(m => m.Address);
                            ViewBag.CurrentSort = "";
                            ViewBag.SortOrder = "desc";
                        }
                        else
                        {
                            customer = customer.OrderBy(m => m.Address);
                            ViewBag.SortOrder = "asc";
                        }
                        break;
    
                    case "Default":
                        ViewBag.OldSort = "CompanyName";
                        customer = customer.OrderBy(m => m.CompanyName);
                        ViewBag.SortOrder = "asc";
                        break;
                }
    
                IPagedList<CustomerDTO> oCustomer = customer.ToPagedList(pageNumber, pageSize);
                return View(oCustomer);
            }

    one similar minimum could i found which is not clear to me. please help me to understand

    ViewBag.OldSort = SortColumn;
    
    var arg = Expression.Parameter(typeof(Customer), "x");
    var exprProp = Expression.Property(arg, SortColumn);
    var projection = Expression.Lambda<Func<Customer, string>>(exprProp, arg);
    
    if (SortColumn.Equals(CurrentSort))
    {
        customer = customer.OrderByDescending(projection);
        ViewBag.CurrentSort = "";
        ViewBag.SortOrder = "desc";
    }
    else
    {
        customer = customer.OrderBy(projection);
        ViewBag.SortOrder = "asc";
    }
    
    

    please explain the above code how it will work?

    also please give me a new set of routine which does the same thing. thanks

    Thursday, February 8, 2018 12:58 PM

Answers

All replies