سؤال

Using some code here:

http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2012/09/19/experimenting-with-windows-azure-mobile-services.aspx

...as "inspiration" for querying a WAMS table, I came up with this:

public static async Task<string> GetMammalDeviceID(string mammalID, string zoologistDeviceID)
{
    IMobileServiceTable<DUCKBILLED_PLATYPI> table = App.MobileService.GetTable<DUCKBILLED_PLATYPI>();

    MobileServiceTableQuery<String> query =
        table.Where(i => i.mammalID == mammalID).
              Where(j => j.zoologistDeviceID == zoologistDeviceID).
              Select(k => k.mammalDeviceID);
    return query.ToString();
}

I question, though, whether this is really "the way to do it" or even a good way to do it. Does anybody have sample code for querying WAMS tables that would set me straight?

Similarly, which of the following is the preferred method for inserting a record?

This:

DUCKBILLED_PLATYPI platypus = new DUCKBILLED_PLATYPI { mammalID = mammalEmail, zoologistDeviceID = zoologistDeviceID, zoologistName = zoologistName };
await App.MobileService.GetTable<DUCKBILLED_PLATYPI>().InsertAsync(platypus);

...or this:

DUCKBILLED_PLATYPI platypus = new DUCKBILLED_PLATYPI { mammalID = mammalEmail, zoologistDeviceID = zoologistDeviceID, zoologistName = zoologistName };
IMobileServiceTable<DUCKBILLED_PLATYPI> platypi = App.MobileService.GetTable<DUCKBILLED_PLATYPI>();
await platypi.InsertAsync(platypus);

?

هل كانت مفيدة؟

المحلول

The query.ToString() will give you the string representation of the underlying mobile service class, something like Microsoft.WindowsAzure.MobileServices.MobileServiceTableQuery``1[System.String] probably not what you want!

I'm assuming you wanted to return the actual mammalDeviceID? If so you need two adjustments,

  1. You can't assume there will only be one row returned, so will need to expect a List<String> not just a String. If you want just one back, you can add Take(1) to the query (but it's still a list).
  2. You need to execute the query by materializing it on the client, in this case, ToListAsync() should work.

So something like:

public static async Task<List<string>> GetMammalDeviceID(string mammalID, string zoologistDeviceID)
{
    IMobileServiceTable<DUCKBILLED_PLATYPI> table = App.MobileService.GetTable<DUCKBILLED_PLATYPI>();

    MobileServiceTableQuery<String> query =
        table.Where(i => i.mammalID == mammalID).
              Where(j => j.zoologistDeviceID == zoologistDeviceID).
              Select(k => k.mammalDeviceID);
    return await query.ToListAsync()
}

As for the second part, there's not difference other than verbosity. I think the fluid nature of the first option is more readable.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top