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

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

问题描述

荫试图分页适用于我的MVC应用程序。使用IAM的Azure表存储

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

下面是我曾尝试: -

Here is what I have tried:-

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;

    }

控制器: -

Controller:-

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项。现在,我怎么能显示并实施previous和下一步,显示下一个页面上的条目的休息吗?如何实现控制器和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?

任何帮助是AP preciated。

Any help is appreciated.

推荐答案

在谈到分页,都需要考虑几件事情:

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


  • 并非所有的LINQ运营商(反过来的OData查询选项),支持餐桌服务。例如跳过不支持。有关支持的运营商的列表,请参阅此链接:<一个href=\"https://msdn.microsoft.com/en-us/library/azure/dd135725.aspx\">https://msdn.microsoft.com/en-us/library/azure/dd135725.aspx.

  • 分页与工作表服务的方式是,当你查询你的表来获取一些数据,可以通过餐桌服务返回实体的最大数量为1000有没有总是1000实体将被退回的保证。这可能是小于1000,甚至0取决于你如何查询。然而,如果有更多的可用结果,表Service返回一种叫做延续标记。您必须使用此令牌来获取下一组从餐桌服务的结果。请参阅此链接的查询超时和分页的更多信息:<一href=\"https://msdn.microsoft.com/en-us/library/azure/dd135718.aspx\">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页)。在最可以实现翻页,previous页和第一页样的功能。

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.

要落实下一页样的功能,存储由表服务返回的延续标记和使用,在您的查询。

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

要落实 previous页样的功能,必须将所有的延续令牌数组或东西返回,并跟踪哪些页面的用户是目前(这将是当前页面索引)。当用户想要去previous页面,你就得到了previous指数延续标记(即当前页索引 - 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.

要落实第一页样的功能,只要你发出查询,而不延续标记。

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

请看看<一个href=\"https://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storage.table.cloudtable.executequerysegmented.aspx\"><$c$c>ExecuteQuerySegmented在存储客户端库的方法,如果你想实现分页。

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

更新

请查看示例code以下。为了简便起见,我只保留第一页和下一页功能:

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\nPress \"F\" to go first page\nPress 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天全站免登陆