添加分页 MVC 和 Azure 表存储 [英] Add Pagination MVC and Azure table storage

查看:10
本文介绍了添加分页 MVC 和 Azure 表存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将分页应用于我的 MVC 应用程序.我正在使用 Azure 表存储

Iam trying to apply Pagination to my MVC application. Iam using Azure table storage

这是我尝试过的:-

public List<T> GetPagination(string partitionKey, int start, int take)
    {
        List<T> entities = new List<T>();

        TableQuery<T> query = new TableQuery<T>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey.ToLower()));
        entities = Table.ExecuteQuery(query).Skip(start).Take(take).ToList();

        return entities;

    }

控制器:-

public ActionResult Index()
        {

            key= System.Web.HttpContext.Current.Request[Constants.Key];
            if (String.IsNullOrEmpty(key))
                return RedirectToAction("NoContext", "Error");

            var items= _StorageHelper.GetPagination(key,0,3);
           ItemCollection itemCollection = new ItemCollection();
            itemCollection .Items= Mapper.Map<List<ItemChart>, List<ItemModel>>(items);
            itemCollection .Items.ForEach(g => g.Object = g.Object.Replace(key, ""));


            return View(itemCollection);
        }

这当前为我提供了数据中的前 3 个条目.现在如何显示和实现上一页"和下一页"以显示下一页上的其余条目?如何实现控制器和 HTML 页面的其余部分?

This currently gives me the first 3 entries from my data. Now how can I show and implement the "Previous" and "Next" to show the rest of the entries on next page? How do I implement the rest of the controller and HTML page?

感谢任何帮助.

推荐答案

说到分页,有几点需要考虑:

When it comes to pagination, there are a few things to consider:

  • 并非所有 LINQ 运算符(以及 OData 查询选项)都支持表服务.例如,不支持 Skip.有关支持的运算符列表,请参阅此链接:https://msdn.microsoft.com/en-us/library/azure/dd135725.aspx.
  • 分页与表服务配合使用的方式是,当您查询表以获取一些数据时,表服务可以返回的最大实体数为 1000.无法保证始终返回 1000 个实体.它可能小于 1000 甚至 0,具体取决于您的查询方式.但是,如果有更多可用结果,表服务会返回一个称为 Continuation Token 的东西.您必须使用此令牌从表服务中获取下一组结果.有关查询超时和分页的详细信息,请参阅此链接:https://msdn.microsoft.com/en-us/library/azure/dd135718.aspx.
  • Not all LINQ operators (and in turn OData query options) are supported for Table Service. For example Skip is not supported. For a list of supported operators, please see this link: https://msdn.microsoft.com/en-us/library/azure/dd135725.aspx.
  • The way pagination works with Table Service is that when you query your table to fetch some data, maximum number of entities that can be returned by table service is 1000. There's no guarantee that always 1000 entities will be returned. It may be less than 1000 or even 0 depending on how you're querying. However if there are more results available, Table Service returns something called a Continuation Token. You must use this token to fetch next set of results from table service. Please see this link for more information on query timeout and pagination: https://msdn.microsoft.com/en-us/library/azure/dd135718.aspx.

考虑到这两个因素,你不能真正实现一个分页解决方案,用户可以直接跳转到特定页面(例如,用户坐在第 1 页,然后用户不能转到第 4 页).您最多可以实现下一页、上一页和第一页的功能.

Taking these two factors into consideration, you can't really implement a paging solution where a user can directly jump to a particular page (for example, user is sitting on page 1 and then the user can't go to page 4). At the most you can implement next page, previous page and first page kind of functionality.

要实现 next page 类型的功能,请存储表服务返回的延续令牌并在您的查询中使用它.

To implement next page kind of functionality, store the continuation token returned by table service and use that in your query.

要实现 previous page 类型的功能,您必须将返回的所有继续标记存储在数组或其他内容中,并跟踪用户当前所在的页面(即当前页面索引).当用户想要转到上一页时,您只需获取上一个索引的继续标记(即当前页索引 - 1)并在查询中使用它.

To implement previous page kind of functionality, you must store all the continuation tokens returned in an array or something and keep track of which page a user is on currently (that would be the current page index). When a user wants to go to previous page, you just get the continuation token for the previous index (i.e. current page index - 1) and use that in your query.

要实现 first page 类型的功能,只需发出您的查询而无需继续令牌.

To implement first page kind of functionality, just issue your query without continuation token.

请查看 ExecuteQuerySegmented存储客户端库中的方法,如果你想实现分页.

Do take a look at ExecuteQuerySegmented method in Storage Client Library if you want to implement pagination.

更新

请参阅下面的示例代码.为了简单起见,我只保留了第一页和下一页的功能:

Please see the sample code below. For the sake of simplicity, I have only kept first page and next page functionality:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Queue;
using Microsoft.WindowsAzure.Storage.Table;
namespace TablePaginationSample
{
    class Program
    {
        static string accountName = "";
        static string accountKey = "";
        static string tableName = "";
        static int maxEntitiesToFetch = 10;
        static TableContinuationToken token = null;
        static void Main(string[] args)
        {
            var cloudStorageAccount = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);
            var cloudTableClient = cloudStorageAccount.CreateCloudTableClient();
            var table = cloudTableClient.GetTableReference(tableName);
            Console.WriteLine("Press "N" to go to next page
Press "F" to go first page
Press any other key to exit program");
            var query = new TableQuery().Take(maxEntitiesToFetch);
            var continueLoop = true;
            do
            {
                Console.WriteLine("Fetching entities. Please wait....");
                Console.WriteLine("-------------------------------------------------------------");
                var queryResult = table.ExecuteQuerySegmented(query, token);
                token = queryResult.ContinuationToken;
                var entities = queryResult.Results;
                foreach (var entity in entities)
                {
                    Console.WriteLine(string.Format("PartitionKey = {0}; RowKey = {1}", entity.PartitionKey, entity.RowKey));
                }
                Console.WriteLine("-------------------------------------------------------------");
                if (token == null)//No more token available. We've reached end of table
                {
                    Console.WriteLine("All entities have been fetched. The program will now terminate.");
                    break;
                }
                else
                {
                    Console.WriteLine("More entities available. Press "N" to go to next page or Press "F" to go first page or Press any other key to exit program.");
                    Console.WriteLine("-------------------------------------------------------------");
                    var key = Console.ReadKey();
                    switch(key.KeyChar)
                    {
                        case 'N':
                        case 'n':
                            continue;
                        case 'F':
                        case 'f':
                            token = null;
                            continue;
                        default:
                            continueLoop = false;
                            break;
                    }
                }
            } while (continueLoop);
            Console.WriteLine("Press any key to terminate the application.");
            Console.ReadLine();
        }
    }
}

这篇关于添加分页 MVC 和 Azure 表存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆