【问题标题】:Need help querying azure mobile service database需要帮助查询 Azure 移动服务数据库
【发布时间】:2015-11-19 15:23:27
【问题描述】:

我正在尝试在 Azure 移动服务数据库中查询特定记录。我需要查询来查找其NotifyDate 列等于当前日期的记录。然后从找到的记录中,我想获取记录的Name 列中的字符串值,并将其存储在可以在数据库外部使用的字符串中。

这是我想出的,但它给了我一个错误:

无法将方法组“ToString”转换为非委托类型“字符串”。 您是否打算调用该方法?

在以下行:

string NotifyDate = FindNotifyDate().ToString;

您能想到更好的方法吗?

当前代码:

private IMobileServiceTable<TodoItem> todoTable =
 MobileService.GetTable<TodoItem>();

private MobileServiceCollection<TodoItem, TodoItem> items;
private List<TodoItem> notifyItems;

protected void Application_Start()
{
    WebApiConfig.Register();
    string NotifyDate = FindNotifyDate().ToString;
}

public async Task<TodoItem> FindNotifyDate()
{
    DateTime test = DateTime.Now;
    test = test.AddMilliseconds(-test.Millisecond);
    notifyItems = await todoTable.Where(todoItem => todoItem.NotifyDate == test)
                                 .ToListAsync();
    return notifyItems[0];
}

【问题讨论】:

    标签: c# azure azure-mobile-services querying


    【解决方案1】:

    试试下面的代码

        protected void async Application_Start()
        {
            WebApiConfig.Register();
            var todoItem = await FindNotifyDate();
            string NotifyDate = todoItem.ToString();
        }
    

    FindNotifyDate 是异步操作。您必须等待它完成才能调用ToString()TodoItem。否则,在您的示例中,您会得到 Task 作为结果类型,这不是您所期望的。使其同步操作以在调用后立即调用ToString() 或等待完成,如上例所示。

    【讨论】:

    • 另外,您应该将 FindNotifyDate() 重命名为 FindNotifyDateAsync() 以遵循 C# 异步约定。
    • 我认为您在 ToString 之后缺少括号。该行应为: string NotifyDate = todoItem.ToString();如果没有括号,您将得到与原始问题相同的错误。
    猜你喜欢
    • 2021-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-29
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    • 2011-04-03
    相关资源
    最近更新 更多