我如何更新我的WAMS表?
本文关键字:我的 WAMS 更新 何更新 | 更新日期: 2023-09-27 17:51:05
认为我需要做的更新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.UpdateAsync(phoneapp . data . wams_邀请函)'有一些无效参数*
…好的,但是什么是无效论证呢?
2)*参数1:不能转换"Microsoft.WindowsAzure.MobileServices.MobileServiceTableQuery
'到' phoneapp . data . wams_请柬'"*
我希望invationrecordtouupdate包含wams_invitation的所有成员/列,包括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,作为两种方式做它(注释掉)不编译。
像这样更新并显示错误:
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
}
编辑我刚刚检查了我的Windows应用程序。我使用这个,一切都很好:
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];
返回列表时只使用第一个元素:
await invitationsTable.UpdateAsync(invitationRecordToUpdate[0]);
你可以做出更好的linq:
invitationsTable.Where(i => i.SenderID == senderID && i.ReaderName == readerName).SingleOrDefault();
记得检查项目是否存在:
if(invitationRecordToUpdate!=null)
{//UPDATE }
SingleOrDefault方法:
返回序列的唯一元素,如果序列为空则返回默认值;如果序列中有多个元素,该方法将抛出异常。