FeedbackScript.Admin.Models.ReportsDomain.GetTrend C# (CSharp) Method

GetTrend() public method

public GetTrend ( string txtDateFrom, string txtDateTo, string urlContains, string SelectedAgency, string SelectedUnit, char CustomOrder ) : IEnumerable
txtDateFrom string
txtDateTo string
urlContains string
SelectedAgency string
SelectedUnit string
CustomOrder char
return IEnumerable
        public IEnumerable<TopPages> GetTrend(string txtDateFrom, string txtDateTo, string urlContains, string SelectedAgency, string SelectedUnit, char CustomOrder)
        {
            try
            {
                var results = dataContext.spGetTrendResults(Convert.ToDateTime(txtDateFrom), Convert.ToDateTime(txtDateTo), urlContains, SelectedAgency, Convert.ToChar(SelectedUnit), CustomOrder);
                IEnumerable<TopPages> res = from resp in results
                                            //orderby resp.Unit descending
                                            select new TopPages
                                            {
                                                UnitName = resp.Unit,
                                                VolumeFeedBack = Convert.ToInt32(resp.VolumeOfFeedback),
                                                NumOfPosComments = Convert.ToInt32(resp.PositiveNoOfComments),
                                                NumOfNegComments = Convert.ToInt32(resp.NegativeNoOfComments)
                                            };

                return res;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

Usage Example

        public ActionResult Trends(string txtDateFrom, string txtDateTo, string txtURLContains, string SelectedAgency, string SelectedUnit, string Export, int? Page, string SortBy, string Order)
        {
            try
            {
                if (txtURLContains == "Enter Text")
                    txtURLContains = "";
                string sUnit = string.Empty;
                if (SelectedUnit == null)
                    SelectedUnit = "D";

                var list = new SelectList(new []
                                {
                                    new {ID="D",Name="Days"},
                                    new{ID="W",Name="Weeks"},
                                    new{ID="M",Name="Months"},
                                    new{ID="Q",Name="Quarters"},
                                    new{ID="Y",Name="Years"},
                                },
                "ID","Name",SelectedUnit);

                ViewData["UnitList"] = list;
                if (SelectedAgency == null)
                {
                    SelectedAgency = "DOL.Gov";
                }
                if (txtURLContains == null)
                {
                    txtURLContains = "";
                }

                int resultsPerPage = 10;

                if (txtDateFrom == null | txtDateTo == null)
                {
                    txtDateTo = System.DateTime.Today.ToShortDateString();
                    txtDateFrom = System.DateTime.Today.Subtract(TimeSpan.FromDays(30)).ToShortDateString();
                }

                ReportsDomain _reportsDomain = new ReportsDomain(ConfigurationManager.ConnectionStrings["FeedbackScriptConnectionString"].ConnectionString);

                bool mFromDateStatus = isDate(txtDateFrom);
                bool mToDateStatus = isDate(txtDateTo);

                if (mFromDateStatus == false)
                {
                    txtDateFrom = System.DateTime.Today.Subtract(TimeSpan.FromDays(30)).ToShortDateString();
                    ModelState.AddModelError("FromDateValidation", "Invalid Date Range");
                }
                if (mToDateStatus == false)
                {
                    txtDateTo = System.DateTime.Today.ToShortDateString();
                    ModelState.AddModelError("ToDateValidation", "Invalid Date Range");
                }

                txtURLContains = ReplaceBad (txtURLContains);
                ViewData["txtDateFrom"] = txtDateFrom;
                ViewData["txtDateTo"] = txtDateTo;
                ViewData["urlContains"] = txtURLContains;

                if (!ModelState.IsValid)
                {
                    ViewData["AgencyList"] = new SelectList(new[]
                                {
                                    new {Agency="Empty List",Val="Empty List"},
                                },
                               "Agency", "Agency", "Empty List");
                    return View();
                }
                else
                {
                    if (Convert.ToDateTime(txtDateFrom) > Convert.ToDateTime(txtDateTo))
                    {
                        ModelState.AddModelError("FromDateValidation", "Invalid Date Range");
                        ModelState.AddModelError("ToDateValidation", "Invalid Date Range");
                        ViewData["AgencyList"] = new SelectList(new[]
                                {
                                    new {Agency="Empty List",Val="Empty List"},
                                },
                                "Agency", "Agency", "Empty List");
                        return View();
                    }
                }

                var orderedResultsPerPage = _reportsDomain.GetTrend(txtDateFrom, txtDateTo, txtURLContains, SelectedAgency, SelectedUnit,'D');

                if (Export == string.Empty | Export == null)
                {
                    if (!Page.HasValue || Page.Value < 1) { Page = 1; }
                    if (SortBy == "" || SortBy == null) { SortBy = "UnitName"; }
                    if (Order == "" || Order == null) { Order = "Asc"; }
                    char mEmpty = new char();
                    PaginationInfo pi = new PaginationInfo { CurrentPage = Page.Value, ResultPerPage = resultsPerPage, TotalCount = _reportsDomain.GetTrend(txtDateFrom, txtDateTo, txtURLContains, SelectedAgency, SelectedUnit,mEmpty).Count(), SortBy = SortBy, Order = Order, txtDateFrom = txtDateFrom, txtDateTo = txtDateTo, SelectedAgency = SelectedAgency, urlContains = txtURLContains, UnitName = SelectedUnit};
                    ViewData["paging"] = pi;

                    switch (SortBy)
                    {
                        case "VolumeOfFeedback":
                            orderedResultsPerPage = Order == "Asc" ? _reportsDomain.GetTrend(txtDateFrom, txtDateTo, txtURLContains, SelectedAgency, SelectedUnit,mEmpty).OrderBy(a => a.VolumeFeedBack) : _reportsDomain.GetTrend(txtDateFrom, txtDateTo, txtURLContains, SelectedAgency, SelectedUnit,mEmpty).OrderByDescending(a => a.VolumeFeedBack);
                            break;
                        case "NumOfPosComments":
                            orderedResultsPerPage = Order == "Asc" ? _reportsDomain.GetTrend(txtDateFrom, txtDateTo, txtURLContains, SelectedAgency, SelectedUnit, mEmpty).OrderBy(a => a.NumOfPosComments) : _reportsDomain.GetTrend(txtDateFrom, txtDateTo, txtURLContains, SelectedAgency, SelectedUnit, mEmpty).OrderByDescending(a => a.NumOfPosComments);
                            break;
                        case "NumOfNegComments":
                            orderedResultsPerPage = Order == "Asc" ? _reportsDomain.GetTrend(txtDateFrom, txtDateTo, txtURLContains, SelectedAgency, SelectedUnit, mEmpty).OrderBy(a => a.NumOfNegComments) : _reportsDomain.GetTrend(txtDateFrom, txtDateTo, txtURLContains, SelectedAgency, SelectedUnit, mEmpty).OrderByDescending(a => a.NumOfNegComments);
                            break;
                        case "UnitName":
                        default:
                            if (SelectedUnit == "M")
                                orderedResultsPerPage = Order == "Asc" ? _reportsDomain.GetTrend(txtDateFrom, txtDateTo, txtURLContains, SelectedAgency, SelectedUnit,'A') : _reportsDomain.GetTrend(txtDateFrom, txtDateTo, txtURLContains, SelectedAgency, SelectedUnit,'D');
                            else
                                orderedResultsPerPage = Order == "Asc" ? _reportsDomain.GetTrend(txtDateFrom, txtDateTo, txtURLContains, SelectedAgency, SelectedUnit, mEmpty).OrderBy(a=>a.UnitName) : _reportsDomain.GetTrend(txtDateFrom, txtDateTo, txtURLContains, SelectedAgency, SelectedUnit, mEmpty).OrderByDescending(a=>a.UnitName);
                            break;
                    }

                    ViewData["SortBy"] = SortBy;
                    ViewData["Order"] = Order;
                    ViewData["AgencyList"] = new SelectList(_reportsDomain.GetAgencyList(txtDateFrom,txtDateTo).ToList(), "Agency", "Agency", SelectedAgency);

                    if (_reportsDomain.GetTrend(txtDateFrom, txtDateTo, txtURLContains, SelectedAgency, SelectedUnit,mEmpty).Count() <= 0)
                    {
                        ModelState.AddModelError("RecordCount", "No results found for the specified search criteria");
                    }
                    else
                    {
                        int MaxRecSizeVolFeedBack = new int();
                        ViewData["graphData"] = GetGraphData(_reportsDomain.GetTrend(txtDateFrom, txtDateTo, txtURLContains, SelectedAgency, SelectedUnit,'A').ToList(), out MaxRecSizeVolFeedBack);
                        ViewData["MaxRecSizeVolFeedBack"] = MaxRecSizeVolFeedBack;
                    }

                    return View(orderedResultsPerPage.Skip(resultsPerPage * (Page.Value - 1)).Take(resultsPerPage));
                }
                else
                {
                    ExportToExcelTrendPage(orderedResultsPerPage.ToList(), txtDateFrom.ToString(), txtDateTo.ToString(),SelectedUnit,txtURLContains,SelectedAgency);
                    return View(orderedResultsPerPage);
                }
            }
            catch (Exception ex)
            {
                ViewData["ErrorMessage"] = ex.Message + ex.StackTrace;
                return View("Error");
            }
        }