Question

I am Developing a Windows 8 App in C# using Azure Mobile Services. This is the first time I have used Azure, I have figured out how to place data into the tables from my app. The thing I can't do is retrieve single pieces of data based on one bit of data in the table. For example retrieve a name from the mobile service based on an ID for instance.

Was it helpful?

Solution

If you want to retrieve the item based on the id, you can use the LookupAsync method:

var table = MobileService.GetTable<MyDataType>();
var item = await table.LookupAsync(id);

If you want to retrieve an item based on another property, you can use a Where clause as @Sopuli mentioned, but you don't need to create a collection for that if all you want is the item itself:

var table = MobileService.GetTable<MyDataType>();
var items = await table.Where(d => d.Property == propValue).ToEnumerableAsync();
var item = items.FirstOrDefault();

OTHER TIPS

To retrieve row from table where id=10000;

int WantedID = 10000;
public IMobileServiceTable<SomeTable> MyTable = App.MobileService.GetTable<SomeTable>();
MobileServiceCollection<SomeTable, SomeTable> MyList = new MobileServiceCollection<SomeTable, SomeTable>(MyTable.Where(t => t.id == WantedID));

And your class SomeTable would be something like:

using Newtonsoft.Json;
...
public class SomeTable: INotifyPropertyChanged
{
    private Int64 _id;
    [JsonProperty(PropertyName = "id")]
    public Int64 id
    {
        get
        {
            return _id;
        }
        set
        {
            if (value != _id)
            {
                _id = value;
                NotifyPropertyChanged("id");
            }
        }
    }

    private string _name;
    [JsonProperty(PropertyName = "name")]
    public string name
    {
        get
        {
            return _name;
        }
        set
        {
            if (value != _name)
            {
                _name= value;
                NotifyPropertyChanged("_name");
            }
        }
    }

Now your MyList is populated with data downloaded from Azure and you can for example bind it to LonglistSelector. And if you need to access property of one specific data item, you can do MyList[0].name

Furthermore I'd advice that your class SomeTable is exact clone of your table in Azure, and each property name is exactly like the column names in the table.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top