简体   繁体   中英

Return Type for Async Method in C#

I am writing a method to delete employees loan schedule.This means I have to delete the result set which is a list. I read that the List.Clear() can delete the list. I have a custom method repository from interface which is async and returns IEnumerable . The problem I have is the return type of the async method after deleting the list.The error is get is

The type arguments for method 'Task.FromResult(TResult)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

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();
}

Hi guys i changed the code again. This time the items in the list were not deleted. Does that mean RemoveAll() does not delete records from the database.The code below does not delete the records.

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

you have some syntax error, you are missing the parameters for Task.FromResult(); passing in a boolean will answer your question.

change your line to: return Task.FromResult(true); will fix your error message.

In addition you can refactor your call to use `await:

public async Task<bool> DeleteEmployeeSchedule(string employeeId, string loantypecode)
{
     var scheduleList = (await _repository.FindBy(e => e.EmployeeId == employeeId && e.LoanTypeCode == loantypecode).ToList();

     scheduleList.Clear()

     //NOTE: however your error occurred on the line below due to wrong syntax, you have to pass in a boolean value eg. true/false
     return Task.FromResult(true);
}  

EDIT : Without seeing more code, for example what datatype your _repository is and how it is implemented, we have to assume too many things... As in one of the comments stated, this will not persist in the database.

EDIT2: @lutakyn posted following code changes as a comment ( please update your initial post with the updated code )

public async Task<bool> DeleteEmployeeSchedule(string employeeId, string loantypecode) 
{
    var scheduleList = (await _repository.FindBy(e => e.EmployeeId == employeeId && e.LoanTypeCode == loantypecode)).ToList();

    scheduleList.RemoveAll(employee => employee.EmployeeId == employeeId);
    await _repository.DbContext.SaveChangesAsync();
    return true;
}

the scheduleList contains the entities matching your criteria, removing them from that list or calling .Clear() will not affect the entities in your DbContext. You have to call on your DbContext the remove like:

_repository.DbContext.Schedule.RemoveRange(scheduleList);

However .Schedule is an assumption since we do not know how your _repository looks like, what datatype the .DbContext exactly is and what your DbSet<> is in your DbContext implementation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM