public PagedResult <GinLoadEntity> GetLoads(GinLoadsFilter filter, int pageSize, int pageNo)
        {
            try
            {
                var filteredQuery = GetFilteredQuery(filter);
                var countQuery    = GetFilteredQuery(filter);

                var cResult = new PagedResult <GinLoadEntity>();

                cResult.Total      = countQuery.Count();
                cResult.TotalPages = cResult.Total / pageSize;
                if (cResult.Total % pageSize > 0)
                {
                    cResult.TotalPages++;
                }
                cResult.LastPageNo = pageNo;
                cResult.ResultData = new List <GinLoadEntity>();
                if (pageNo <= cResult.TotalPages)
                {
                    cResult.ResultData.AddRange(filteredQuery.Skip(pageSize * (pageNo - 1)).Take(pageSize).ToList());
                }
                return(cResult);
            }
            catch (Exception exc)
            {
                Logging.Logger.Log(exc);
                throw new Exception("Error fetching gin loads.", exc);
            }
        }
        private IOrderedQueryable <GinLoadEntity> GetFilteredQuery(GinLoadsFilter filter)
        {
            string farmName   = filter.Farm.Trim();
            string clientName = filter.Client.Trim();
            string fieldName  = filter.Field.Trim();

            DateTime?start = null;

            if (filter.StartDate.HasValue)
            {
                start = filter.StartDate.Value;
            }

            DateTime?end = null;

            if (filter.EndDate.HasValue)
            {
                end = filter.EndDate.Value;
            }

            var filteredQuery = _context.GinLoads.Include("Field.Farm.Client").AsQueryable();

            if (!string.IsNullOrEmpty(filter.BridgeId))
            {
                filteredQuery = filteredQuery.Where(c => c.ScaleBridgeId == filter.BridgeId);
            }

            if (filter.StartDate.HasValue)
            {
                filteredQuery = filteredQuery.Where(c => c.Created >= start.Value);
            }

            if (filter.EndDate.HasValue)
            {
                filteredQuery = filteredQuery.Where(c => c.Created <= end.Value);
            }

            if (!string.IsNullOrEmpty(fieldName))
            {
                filteredQuery = filteredQuery.Where(c => c.Field.Name == fieldName);
            }

            if (!string.IsNullOrEmpty(farmName))
            {
                filteredQuery = filteredQuery.Where(c => c.Field.Farm.Name == farmName);
            }

            if (!string.IsNullOrEmpty(clientName))
            {
                filteredQuery = filteredQuery.Where(c => c.Field.Farm.Client.Name == clientName);
            }

            if (!string.IsNullOrEmpty(filter.GinTagLoadNumber))
            {
                filteredQuery = filteredQuery.Where(c => c.GinTagLoadNumber == filter.GinTagLoadNumber);
            }

            if (!string.IsNullOrEmpty(filter.BridgeLoadNumber))
            {
                int temp = 0;

                if (int.TryParse(filter.BridgeLoadNumber, out temp))
                {
                    filteredQuery = filteredQuery.Where(c => c.ScaleBridgeLoadNumber == temp);
                }
            }

            IOrderedQueryable <GinLoadEntity> orderable = null;

            if (filter.Sort1Ascending)
            {
                if (filter.SortCol1.ToLower() == "client")
                {
                    orderable = filteredQuery.OrderBy(m => m.Field.Farm.Client.Name);
                }
                else if (filter.SortCol1.ToLower() == "farm")
                {
                    orderable = filteredQuery.OrderBy(m => m.Field.Farm.Name);
                }
                else if (filter.SortCol1.ToLower() == "field")
                {
                    orderable = filteredQuery.OrderBy(m => m.Field.Name);
                }
                else if (filter.SortCol1.ToLower() == "scale bridge id")
                {
                    orderable = filteredQuery.OrderBy(m => m.Name);
                }
                else if (filter.SortCol1.ToLower() == "gin tag load #")
                {
                    orderable = filteredQuery.OrderBy(m => m.GinTagLoadNumber);
                }
                else if (filter.SortCol1.ToLower() == "scale bridge load #")
                {
                    orderable = filteredQuery.OrderBy(m => m.ScaleBridgeLoadNumber);
                }
                else if (filter.SortCol1.ToLower() == "timestamp")
                {
                    orderable = filteredQuery.OrderBy(m => m.Created);
                }
                else if (filter.SortCol1.ToLower() == "created")
                {
                    orderable = filteredQuery.OrderBy(m => m.Created);
                }
                else if (filter.SortCol1.ToLower() == "updated")
                {
                    orderable = filteredQuery.OrderBy(m => m.Updated);
                }
            }
            else
            {
                if (filter.SortCol1.ToLower() == "client")
                {
                    orderable = filteredQuery.OrderByDescending(m => m.Field.Farm.Client.Name);
                }
                else if (filter.SortCol1.ToLower() == "farm")
                {
                    orderable = filteredQuery.OrderByDescending(m => m.Field.Farm.Name);
                }
                else if (filter.SortCol1.ToLower() == "field")
                {
                    orderable = filteredQuery.OrderByDescending(m => m.Field.Name);
                }
                else if (filter.SortCol1.ToLower() == "scale bridge id")
                {
                    orderable = filteredQuery.OrderByDescending(m => m.Name);
                }
                else if (filter.SortCol1.ToLower() == "gin tag load #")
                {
                    orderable = filteredQuery.OrderByDescending(m => m.GinTagLoadNumber);
                }
                else if (filter.SortCol1.ToLower() == "scale bridge load #")
                {
                    orderable = filteredQuery.OrderByDescending(m => m.ScaleBridgeLoadNumber);
                }
                else if (filter.SortCol1.ToLower() == "timestamp")
                {
                    orderable = filteredQuery.OrderByDescending(m => m.Created);
                }
                else if (filter.SortCol1.ToLower() == "created")
                {
                    orderable = filteredQuery.OrderByDescending(m => m.Created);
                }
                else if (filter.SortCol1.ToLower() == "updated")
                {
                    orderable = filteredQuery.OrderByDescending(m => m.Updated);
                }
            }

            return(orderable);
        }