2015-08-25 47 views
0

我想查詢某個特定記錄的azure移動服務數據庫。我需要查詢來查找NotifyDate列等於當前日期的記錄。然後從找到的記錄中,我想獲取記錄的Name列中的字符串值,並將其存儲在我可以在數據庫之外使用的字符串中。需要幫助查詢azure移動服務數據庫

這是我想出的,但它給我一個錯誤:

Cannot convert method group 'ToString' to non delegate type 'string'. Did you intend to invoke the method?

在下面一行:

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]; 
} 
+0

這有什麼好運氣? – Artiom

回答

1

試試下面的代碼

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

FindNotifyDate是異步操作。您必須等待完成才能撥打TodoItemToString()。否則在你的例子中,你得到Task作爲結果類型,這不是你所期望的。如同上述樣品一樣,在呼叫完成或等待完成後立即進行同步操作以呼叫ToString()

+1

另外,您應該重命名FindNotifyDate()以FindNotifyDateAsync()遵循C#異步約定。 –

+0

我想你錯過了ToString後面的括號。該行應爲:string NotifyDate = todoItem.ToString();沒有括號,你會得到與原始問題相同的錯誤。 – WiteCastle

+0

@WiteCastle thx,已修復 – Artiom