简体   繁体   English

输出Azure表中的实体数

[英]Outputting the number of entities in an Azure table

I'm having this issue that I've been going crazy over for a few hours now. 我遇到了这个问题,我已经疯了几个小时了。 I've got a table stored in Azure that holds tasks a user wants to accomplish. 我有一个存储在Azure中的表,其中包含用户想要完成的任务。 Each task has a status which can be an integer value of either 1, 2 or 3 corresponding to to do, in progress or completed respectively. 每个任务的状态可以分别是1、2或3的整数值,分别对应于待执行,正在进行或已完成。 I'm trying to create three separate gauges which tell the user how many tasks they have in each of those three categories, but everything I've tried throws a MobileServiceInvalidOperation error and I can't seem to figure out why. 我正在尝试创建三个单独的仪表,以告诉用户这三个类别中每个任务有多少个任务,但是我尝试执行的所有操作都会引发MobileServiceInvalidOperation错误,而且我似乎无法弄清楚原因。

--EDIT-- - 编辑 -

I was getting a similar issue in another part of my program, and the issue turned out to be one of permissions. 我在程序的另一部分遇到了类似的问题,该问题原来是权限之一。 Once I included the user authentication code that I had used on another page, the queries I was running on that page started to work. 一旦包含了我在其他页面上使用过的用户身份验证代码,我在该页面上运行的查询就开始起作用。 Including that code however, did not stop this exception from being thrown on the code below. 但是,包括该代码并不能阻止将此异常引发到下面的代码中。 I now think that the issue has to do with this code running before the app authenticates the user. 我现在认为问题与应用验证用户身份之前运行的代码有关。 If this is the case, how would I go about making sure this code runs after the authentication code does? 如果是这种情况,我将如何确保此代码在身份验证代码运行之后运行?

--END EDIT-- -结束编辑-

I'll list out some of my attempts below: 我将在下面列出一些尝试:

private MobileServiceCollection<SbTask, SbTask> taskItems;
private IMobileServiceTable<SbTask> taskTable = App.MobileService.GetTable<SbTask>();

Attempt 1) 尝试1)

private async void GetTodo()
{
    // Can't convert IMobileServiceTableQuery<Int> to 
    // IMobileServiceQuery<AppName.Models.Sbtask>
    IMobileServiceTableQuery<SbTask> query = taskTable
        .Where(t => t.Status == 1)
        .Select(t => t.Status);
}

Attempt 2) 尝试2)

private async void GetTodo()
{
    IMobileServiceTableQuery<int> query = taskTable
        .Where(t => t.Status == 1)
        .Select(t => t.Status);

    // MobileServiceInvalidOperationException
    // Additional information: Error: Unauthorized
    List<int> statusOne = await query.ToListAsync();

    Value = statusOne.Count;  // Also tried statusOne.Sum();
}

Attempt 3) 尝试3)

private async void GetTodo()
{
    var query = taskTable
        .Where(t => t.Status == 1)
        .Select(t => t.Status)
        .IncludeTotalCount();

    // MobileServiceInvalidOperationException
    // Additional information: Error: Unauthorized
    var results = await query.ToEnumerableAsync();

    long count = ((ItotalCountProvider)results).TotalCount;
    int counts = Int32.Parse(count.ToString());

    List<int> stats = await query.ToListAsync();

    for (int i = 0; i < counts; i++)
    {
        Value += stats[i];
    }
}

Attempt 4) 尝试4)

private async void GetTodo()
{
    var query = taskTable
        .Where(t => t.Status == 1)
        .Select(t => t.Status)
        .IncludeTotalCount();

    // MobileServiceInvalidOperationException
    // Additional information: Error: Unauthorized
    List<int> stats = await query.ToListAsync();

    long count = ((ItotalCountProvider)stats).TotalCount;
    int counts = Int32.Parse(count.ToString());

    for (int i = 0; i < counts; i++)
    {
        Value += stats[i];
    }
}

Attempt 5) 尝试5)

private async void GetTodo()
{
    // MobileServiceInvalidOperationException
    // Additional information: Error: Unauthorized
    taskItems = await taskTable.ToCollectionAsync();

    Value = taskItems.Count(t => t.Status == 1);
}

I've tried a couple of other things that probably aren't even worth mentioning, and I've been searching all over for solutions to this problem--all's I've really learned is that this is a difficult problem and I (obviously) haven't really found a solution. 我尝试了其他一些甚至可能都不值得一提的事情,并且我一直在寻找解决该问题的方法-我真正了解到的是,这是一个困难的问题,我(显然)尚未真正找到解决方案。

Thanks in advance to anyone who can help. 在此先感谢任何可以提供帮助的人。

So it turns out that the issue was related to authentication. 因此,事实证明该问题与身份验证有关。 Moving the GetTodo() method out of my viewmodel class and into the xaml.cs file did the trick. 将GetTodo()方法从我的viewmodel类中移出到xaml.cs文件中就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM