检查Task <bool>是true还是false(C#)

Eug*_*ukh -2 c# task-parallel-library

我有方法,要检查房东的数目?房东类型

我有这段代码来检查它

var type = _landlordTypeRepository.GetAll()
    .Include(x => x.Landlords)
    .FirstOrDefault(x => x.Id == input.Id);

if (type.Landlords.Count > 0)
{
    throw new UserFriendlyException(L("ThisTypeIsInUse"));
}
Run Code Online (Sandbox Code Playgroud)

但是我这样改写了

var type = _landlordTypeRepository
    .GetAll()
    .AnyAsync(x=> x.Id == input.Id && x.Landlords.Any());
Run Code Online (Sandbox Code Playgroud)

但是现在返回类型ID Task<bool>

如果我该如何使用呢?

Nic*_*ick 5

您需要使用await

var type = await _landlordTypeRepository.GetAll().AnyAsync(x=> x.Id == input.Id && x.Landlords.Any());
Run Code Online (Sandbox Code Playgroud)

您的方法还必须标记为async

我建议您熟悉异步等待