Question

Thinking that what I need to do to update a WAMS record is to select the record I want, then modify the member of the selected record that I want to change, and finally call Update on that record, I've got this code:

 public static async Task<bool> UpdateInvitation(string senderID, string readerName, string senderDeviceID)
 {
     try
     {
         IMobileServiceTable<WAMS_INVITATIONS> invitationsTable = App.MobileService.GetTable<WAMS_INVITATIONS>();
         MobileServiceTableQuery<WAMS_INVITATIONS> invitationRecordToUpdate =
               invitationsTable.Where(i => i.SenderID == senderID).
                                Where(i => i.ReaderName == readerName).
                                //Select(i => i.SenderDeviceID).
                                Take(1); // the query returns a List, but I only want 1 record (and it should only return 1)
         //invitationRecordToUpdate.SenderDeviceID = senderDeviceID; <-- "SenderDeviceID" not recognized
         await invitationsTable.UpdateAsync(invitationRecordToUpdate);
     }
     catch (Exception)
     {
         return false;
     }
     return true; // If no exception, assume record was inserted successfully
 }

...but it won't even compile. The err msgs I get are:

1) *The best overloaded method match for 'Microsoft.WindowsAzure.MobileServices.IMobileServiceTable.Upd ateAsync(PhoneApp.Data.WAMS_INVITATIONS)' has some invalid arguments*

...okay, but what are the invalid arguments?

2) *Argument 1: cannot convert from 'Microsoft.WindowsAzure.MobileServices.MobileServiceTableQuery

' to 'PhoneApp.Data.WAMS_INVITATIONS'"*

I would expect invitationRecordToUpdate to contain all the members/columns of WAMS_INVITATIONS, including SenderDeviceID, but no...why? And more importantly, what is the canonical/preferred method (no pun intended) to update a WAMS record?

UPDATE

(No pun intended, although this question does revolve around Updates)

Trying to use the suggested answer, this wont' compile:

invitationsTable.Where(i => i.SenderID == senderID && i.ReaderName ==  readerName).SingleOrDefault
();

(in the VS editor, the "SingleOrDefault" is redder than chairman Mao's book)

...and this:

await invitationsTable.UpdateAsync(invitationRecordToUpdate[0]);

...also does not compute, with: "Cannot apply indexing with [] to an expression of type 'Microsoft.WindowsAzure.MobileServices.MobileServiceTableQuery'"

UPDATE 2

Changing it from:

IMobileServiceTable<TASLS_WAMS_INVITATIONS> invitationsTable = App.MobileService.GetTable<TASLS_WAMS_INVITATIONS>();

...to this:

var invitationsTable = App.MobileService.GetTable<TASLS_WAMS_INVITATIONS>();

...makes no difference (and it shouldn't - all that does is make the explicity data type implicit based on the type's constructor.

So my code is now:

public static async Task<bool> UpdateInvitation(string senderID, string readerName, string senderDeviceID)
{
    try
    {
        IMobileServiceTable<TASLS_WAMS_INVITATIONS> invitationsTable = App.MobileService.GetTable<TASLS_WAMS_INVITATIONS>();

        MobileServiceTableQuery<TASLS_WAMS_INVITATIONS> invitationRecordToUpdate =
            invitationsTable.Where(i => i.SenderID == senderID).
                             Where(i => i.ReaderName == readerName).
                             Take(1);

        if (null != invitationRecordToUpdate)
        {
            //await invitationsTable.UpdateAsync(invitationRecordToUpdate[0]); //both of these fail...
            //await invitationsTable.UpdateAsync(invitationRecordToUpdate);
        }
    }
    catch (Exception)
    {
        return false;
    }
    return true; // If no exception, assume record was inserted successfully
}

...as you can see, I'm unable to UpdateAsync, as both ways of doing it (commented out) don't compile.

Was it helpful?

Solution

Update like this and show error:

public static async Task<bool> UpdateInvitation(string senderID, string readerName, string senderDeviceID)
    {
        try
        {
            IMobileServiceTable<TASLS_WAMS_INVITATIONS> invitationsTable = App.MobileService.GetTable<TASLS_WAMS_INVITATIONS>();

            var invitationRecordToUpdate =
                invitationsTable.Where(i => i.SenderID == senderID).
                                 Where(i => i.ReaderName == readerName).
                                 Take(1).ToListAsync();

                await invitationsTable.UpdateAsync(invitationRecordToUpdate[0]);
        }
        catch (Exception)
        {
            return false;
        }
        return true; // If no exception, assume record was inserted successfully
    }

EDIT I just checked my Windows stre app. I use this and everything is ok:

private IMobileServiceTable<Rating> RatingTable =
         App.MobileService.GetTable<Rating>();

    var list = await RatingTable.Where(p => p.Id == sel_item.Id && p.User == user).ToListAsync();
    vat item=list[0];

OTHER TIPS

When return list just use first element:

       await invitationsTable.UpdateAsync(invitationRecordToUpdate[0]);

You can make better linq:

invitationsTable.Where(i => i.SenderID == senderID && i.ReaderName ==  readerName).SingleOrDefault();

Remember to chek item exist:

if(invitationRecordToUpdate!=null)
{//UPDATE }

SingleOrDefault method:

Returns the only element of a sequence, or a default value if the sequence is empty; this method throws an exception if there is more than one element in the sequence.

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