如何更新我的WAMS表?

时间:2013-01-09 19:03:52

标签: c# sql-update windows-phone-8 azure-sql-database azure-mobile-services

认为我需要做的是更新WAMS记录是选择我想要的记录,然后修改我要更改的所选记录的成员,最后在该记录上调用Update,我有这个代码:

 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
 }

...但它甚至不会编译。我得到的错误信息是:

1) *最佳重载方法匹配'Microsoft.WindowsAzure.MobileServices.IMobileServiceTable.Upd ateAsync(PhoneApp.Data.WAMS_INVITATIONS)'有一些无效的参数*

......好吧,但是无效的论点是什么?

2) *参数1:无法转换 “Microsoft.WindowsAzure.MobileServices.MobileServiceTableQuery

  

'到'PhoneApp.Data.WAMS_INVITATIONS'“*

我希望invitationRecordToUpdate包含WAMS_INVITATIONS的所有成员/列, 包括SenderDeviceID,但没有......为什么?更重要的是,什么是规范/首选方法 (没有双关语)更新WAMS记录?

更新

(没有双关语意,虽然这个问题的确围绕着更新)

尝试使用建议的答案,这不会'编译:

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

(在VS编辑器中,“SingleOrDefault”比毛主席的书更红)

......而且这个:

await invitationsTable.UpdateAsync(invitationRecordToUpdate[0]);

...也无法计算,但是:“无法将带有[]的索引应用于类型为”Microsoft.WindowsAzure.MobileServices.MobileServiceTableQuery“的表达式

更新2

将其改为:

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

......对此:

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

...没有区别(它不应该 - 所有这一切都是根据类型的构造函数隐式显式数据类型。

所以我的代码现在是:

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
}

...正如您所看到的,我无法使用UpdateAsync,因为这两种方式(注释掉)都不会编译。

2 个答案:

答案 0 :(得分:1)

当返回列表只使用第一个元素时:

       await invitationsTable.UpdateAsync(invitationRecordToUpdate[0]);

你可以做出更好的linq:

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

记得chek项目存在:

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

SingleOrDefault方法:

  

返回序列的唯一元素,如果序列为空,则返回默认值;如果序列中有多个元素,则此方法抛出异常。

答案 1 :(得分:1)

像这样更新并显示错误:

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 我刚检查了我的Windows Stre应用程序。我用这个,一切都很好:

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