Beispiel #1
0
        public async Task <Customer[]> Run()
        {
            var query     = _customerQueries.GetCustomersQuery();
            var customers = await query.ToArrayAsync();

            return(customers);
        }
Beispiel #2
0
        public async Task <GetCustomersResponse> GetCustomersV2([FromUri] GetCustomersRequest request)
        {
            var customersQuery = _customerQueries.GetCustomersQuery();

            // 建立篩選條件
            var filterExpr = CreateFilterExpression(request);

            customersQuery = customersQuery
                             .AsExpandable() // 必須先呼叫此擴充方法(來自 LinqKit)。
                             .Where(filterExpr);

            // 排序
            switch (request.SortOrder.ToLower())
            {
            case "country":
            case "country asc":
                customersQuery = customersQuery.OrderBy(c => c.Country);
                break;

            case "country desc":
                customersQuery = customersQuery.OrderByDescending(c => c.Country);
                break;

            case "name desc":
                customersQuery = customersQuery.OrderByDescending(c => c.Name);
                break;

            default:
                customersQuery = customersQuery.OrderBy(c => c.Name);
                break;
            }

            // 分頁
            var pagedResult = await customersQuery.ToPagedListAsync(request.Page, request.PageSize);

            // 轉換成 View Model
            var customerModels = pagedResult.Select(ConvertToCustomerViewModel);

            return(new GetCustomersResponse
            {
                Succeeded = true,
                Customers = customerModels.ToArray()
            });
        }