Outputting the number of entities in an Azure table

孤街浪徒 提交于 2019-12-12 02:26:35

问题


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. 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. 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.

--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)

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)

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)

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)

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)

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.


回答1:


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.



来源:https://stackoverflow.com/questions/25474663/outputting-the-number-of-entities-in-an-azure-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!