【发布时间】:2019-04-05 08:23:41
【问题描述】:
我正在编写一个删除员工贷款计划的方法。这意味着我必须删除作为列表的结果集。我读到 List.Clear() 可以删除列表。我有一个来自接口的自定义方法 repository,它是异步的并返回 IEnumerable 。我遇到的问题是删除列表后异步方法的返回类型。错误是get is
方法“Task.FromResult(TResult)”的类型参数无法从用法中推断出来。尝试明确指定类型参数。
public Task<bool> DeleteEmployeeSchedule(string employeeId, string loantypecode)
{
var scheduleList = _repository.FindBy(e => e.EmployeeId == employeeId && e.LoanTypeCode == loantypecode).Result.ToList();
scheduleList.Clear()
return Task.FromResult();
}
大家好,我再次更改了代码。这次列表中的项目没有被删除。这是否意味着 RemoveAll() 不会从数据库中删除记录。下面的代码不会删除记录。
public async Task<bool> DeleteEmployeeSchedule(string employeeId, string loantypecode)
{
var scheduleList = _repository.FindBy(e => e.EmployeeId == employeeId && e.LoanTypeCode == loantypecode).Result.ToList();
scheduleList.RemoveAll(employee => employee.EmployeeId == employeeId);
await _repository.DbContext.SaveChangesAsync();
return true;
}
【问题讨论】:
-
没有人从不阅读异常消息。 无法从用法推断方法“Task.FromResult(TResult)”的类型参数意味着编译器无法从用法推断方法“Task.FromResult(TResult)”的类型参数。
-
你的
Task.FromResult()没有参数,它必须有bool类型的参数。你怎么能从scheduleList返回bool? -
List.Clear()只会清除List的内容。它不会从源_repository中删除记录。您的代码正在创建一个新列表,然后将其清除,但它没有进行任何有意义的更改。 -
使用 removerange 或 remove all 怎么样?
-
@lutakyn,你能展示你的存储库吗?
标签: c# entity-framework asp.net-mvc-5