Esempio n. 1
0
        public async Task <IEnumerable <Vehicle> > GetVehicles(VehicleQuery queryObject)
        {
            var query = context.Vehicles
                        .Include(v => v.Model)
                        .ThenInclude(m => m.Make)
                        .Include(v => v.Features)
                        .ThenInclude(vf => vf.Feature)
                        .AsQueryable();

            if (queryObject.MakeId.HasValue)
            {
                query = query.Where(v => v.Model.MakeId == queryObject.MakeId);
            }

            var columnsMap = new Dictionary <string, Expression <Func <Vehicle, object> > >
            {
                ["make"]        = v => v.Model.Make.Name,
                ["model"]       = v => v.Model.Name,
                ["contactName"] = v => v.ContactName,
                ["id"]          = v => v.Id
            };

            query = IQueryableExtensions <Vehicle> .ApplyOrdering(query, queryObject, columnsMap);

            query = IQueryableExtensions <Vehicle> .ApplyPaging(query, queryObject);

            return(await query.ToListAsync());
        }
Esempio n. 2
0
        public async Task <IEnumerable <Vehicle> > GetVehicles(Filter filter)
        {
            var query = _context.Vehicles
                        .Include(v => v.VehicleFeatures)
                        .ThenInclude(vf => vf.Feature)
                        .Include(v => v.Model)
                        .ThenInclude(m => m.Make).AsQueryable();

            if (filter.MakeId.HasValue)
            {
                query = query.Where(v => v.Model.MakeId == filter.MakeId);
            }

            if (filter.ModelId.HasValue)
            {
                query = query.Where(v => v.ModelId == filter.ModelId);
            }

            // Sorting with Dictionary.
            var columnsMap = new Dictionary <string, Expression <Func <Vehicle, object> > >()
            {
                ["make"]        = v => v.Model.Make.Name,
                ["model"]       = v => v.Model.Name,
                ["contactName"] = v => v.ContactName,
                ["id"]          = v => v.Id
            };

            query = IQueryableExtensions.ApplyOrdering(filter, query, columnsMap);


            // Sorting without Dictionary.

            /*if (filter.SortBy == "make")
             * {
             *  query = filter.IsSortAscending ? query.OrderBy(v => v.Model.Make.Name) : query.OrderByDescending(v => v.Model.Make.Name);
             * }
             * if (filter.SortBy == "model")
             * {
             *  query = filter.IsSortAscending ? query.OrderBy(v => v.Model.Name) : query.OrderByDescending(v => v.Model.Name);
             * }
             * if (filter.SortBy == "contactName")
             * {
             *  query = filter.IsSortAscending ? query.OrderBy(v => v.ContactName) : query.OrderByDescending(v => v.ContactName);
             * }
             * if (filter.SortBy == "id")
             * {
             *  query = filter.IsSortAscending ? query.OrderBy(v => v.Id) : query.OrderByDescending(v => v.Id);
             * }*/

            return(await query.ToListAsync());
        }