Roroto
Roroto

Reputation: 91

Windows Azure Mobile Service query table

I use Windows Azure Mobile Service. I have a table of Element. I want to query the cloud database :

But I don't understand at all the "query" system with Windows Azure Mobile Service. I have a IMobileServiceTable but don't know what to do with that...

I checked on tutorial, and they explain how to use Where clause, but not select. And I need to select only some column because my element have picture and I don't want to download it in my getAll method....

Edit :

I try that :

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

But it doesn't work.

Upvotes: 0

Views: 6855

Answers (3)

David Yang
David Yang

Reputation: 31

It sounds like you are just looking to do a simple query with IMobileServiceTable

SELECT Id, Name FROM Element ORDER BY creationTime

If you do not mind using the IMobileServiceTable<TodoItem>, you can try:

1) Removing the member properties you do not need from your Object

Example:

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) Here's the code to read the data:

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

which is basically:

SELECT TOP 10 Id, Complete FROM TodoTable ORDER BY Id

The code example for todoTable is at http://www.windowsazure.com/en-us/develop/mobile/tutorials/get-started-wp8/

Hope this helps.

Upvotes: 1

Mlunes
Mlunes

Reputation: 481

You can find a helpful post from Carlos that includes what the corresponding SQL query would be here: http://blogs.msdn.com/b/carlosfigueira/archive/2012/09/21/playing-with-the-query-object-in-read-operations-on-azure-mobile-services.aspx

For example:

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

woudld translate to

SELECT TOP 10 [id], [MovieName], [MovieRating] FROM MovieRating WHERE Rating > 2 AND UserId = ? ORDER BY MovieName

So for your case where you need to translate

SELECT Id, Name FROM Element ORDER BY creationTime

you'd go with something like the following:

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

Upvotes: 1

Yossi Dahan
Yossi Dahan

Reputation: 5377

If you're using .net, you pretty much follow linq. Looking at the sample app - where it has -

    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;
    }

If, for example, you did not want to return the Complete flag you could add before the call to .ToCollectionView()

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

Which would create a list of a new object of anonymous type (can be a concrete type) with the two members specified.

Upvotes: 0

Related Questions