我是新來使用await /異步,我有一個基本的問題。我能夠在我的WebApi控制器和業務對象(BL)中成功實現await/async模型以刪除,因此我在Delete
方法中調用了await entities.SaveChangesAsync();
,並更新了方法的簽名以返回public static async Task<ProfileItem> Delete(int profileId, int profileItemId)
,並且這個工作正常!當我「獲取」數據時,我想要做同樣的事情,所以,我有一個「getter」,我更新了方法的簽名到public static async Task<List<Property>> GetProperties(int userId, int userTypeId)
,這裏我有一些邏輯(1)使用實體框架來檢索結果集,然後我做一些東西,並將我的EntityObject轉換爲BusinessObject並返回List<Property>
,但是當我做return await ...
時出現錯誤:列表不包含'GetAwaiter'的定義並且沒有擴展方法...C#GetAwaiter異步任務<T>
下面的代碼
public static async Task<List<Property>> GetProperties(int userId, int userTypeId)
{
entities = new MyEntities();
var userType = entities.sl_USER_TYPE.Where(_userType => _userType.ID == userTypeId).First();
var properties = entities.sl_PROPERTY.Where(_property => _property.USER_ID == userId && _property.USER_TYPE_ID == userTypeId);
if (!properties.Any())
throw new Exception("Error: No Properties exist for this user!");
// here is where I get the error
return await ConvertEntiesToBusinessObj(properties.ToList(), userId);
}
什麼我需要做的是能夠訪問是在這種情況下,這個功能的功能。基本上我可以使用任務/異步將信息保存到數據庫但沒有得到。我相信這是我缺乏理解。
感謝。
的問題是'ConvertEntiesToBusinessObj'請顯示該方法的代碼 – CodingYoshi
'VAR列表=等待properties.ToListAsync();。如果(list.Count == 0){拋出新的Exception(「Error:No Properties exists for this user!」);} else {return ConvertEntiesToBusinessObj(list,userId);}'如果你要檢索元素,那麼不需要單獨執行'Any另外,你可以使用'等待FirstAsync()'而不是'First()'。 – PetSerAl