2013-02-13 121 views
0

我使用Windows Azure移動服務。 我有一個元素表。 我要查詢的雲數據庫:Windows Azure移動服務查詢表

  • 選擇ID,名稱 從單元ORDER BY創建時間

但我不會在所有的「查詢」使用Windows Azure移動服務系統瞭解。 我有一個IMobileServiceTable,但不知道該怎麼辦...

我檢查了教程,他們解釋如何使用Where子句,但不選擇。我需要選擇只有某些列,因爲我的元素有圖片,我不想下載它在我GETALL方法....

編輯:

我嘗試:

Task.Factory.StartNew(() => 
{ 
    var query = table.Select(x => 
       new Element() 
       { 
        Id = x.Id, 
        Name = x.Name, 
        Price = x.Price 
       }); 
    var _items = query.ToListAsync().Result; 
}).ContinueWith((x) => handleProductsArrived(x.Result)); 

但它不起作用。

+0

你使用的是.net還是javascript? – 2013-02-13 16:06:42

+0

.net(xamarin,c#) – Roroto 2013-02-15 11:07:44

回答

0

如果你使用.net,你幾乎可以遵循linq。 在研究樣本應用程序 - 它有 -

private void RefreshTodoItems() 
    { 
     // This code refreshes the entries in the list view be querying the TodoItems table. 
     // The query excludes completed TodoItems 
     items = todoTable 
      .Where(todoItem => todoItem.Complete == false) 
      .ToCollectionView(); 
     ListItems.ItemsSource = items; 
    } 

如果,例如,你不希望你可以在電話前加上完整的標誌返回.ToCollectionView()

.Select(item=>new {item.Id, item.Text}) 

這將創建指定兩個成員的匿名類型的新對象(可以是具體類型)的列表。

+0

我在第一篇文章中添加了一些細節。 – Roroto 2013-02-15 09:50:37

1

你可以找到卡洛斯一個有用的職位,其中包括相應的SQL查詢是什麼在這裏:http://blogs.msdn.com/b/carlosfigueira/archive/2012/09/21/playing-with-the-query-object-in-read-operations-on-azure-mobile-services.aspx

例如:

function read(query, user, request) { 
query.where({ UserId: user.userId }) 
    .select('id', 'MovieName', 'MovieRating') 
    .orderBy('MovieName') 
    .take(10); 
request.execute(); 
} 

woudld轉化爲

SELECT TOP 10 [ID ],[MovieName],[MovieRating] FROM MovieRating WHERE Rating> 2和UserId =? ORDER BY的movieName

所以對於你的情況,你需要翻譯

選擇ID,名稱 從單元 ORDER BY創建時間

你會去的東西,如下列:

function read(query, user, request) { 
    query.where({ UserId: user.userId }) 
     .select('id', 'Name', 'Element') 
     .orderBy('creationTime') 
    request.execute(); 
} 
+0

這是WAMS(windows azure移動服務)。如何直接在C#上做到這一點? – Roroto 2013-02-15 09:49:54

+0

嘗試使用mssql對象:http://msdn.microsoft.com/en-us/library/windowsazure/jj554212.aspx – Mlunes 2013-02-15 19:29:44

+0

目標不是使用直接連接到sql =>將安全憑證放在裏面不是一件好事解。 – Roroto 2013-02-18 08:17:20

1

這聽起來像你只是想用IMobileServiceTable做一個簡單的查詢

SELECT Id, Name FROM Element ORDER BY creationTime 

如果你不介意使用IMobileServiceTable<TodoItem>,你可以嘗試:

1)拆除成員屬性,你不會從你的對象需要

例子:

public class TodoItem 
{ 
    public int Id { get; set; } 

    // REMOVE WHAT YOU DO NOT WANT 
    //[DataMember(Name = "text")] 
    //public string Text { get; set; } 

    [DataMember(Name = "complete")] 
    public bool Complete { get; set; } 
} 

2)以下是讀取數據的代碼:

private void RefreshTodoItems() 
{ 
    items = todoTable 
      .OrderBy(todoItem => todoItem.Id) 
      .Take(10) 
      .ToCollectionView(); 
    ListItems.ItemsSource = items; 
} 

這是基本上是:

SELECT TOP 10 Id, Complete FROM TodoTable ORDER BY Id 

爲todoTable的代碼示例是在http://www.windowsazure.com/en-us/develop/mobile/tutorials/get-started-wp8/

希望這有助於。