简体   繁体   English

检查是否有任务<bool>是真还是假(C#)

[英]Check if Task<bool> is true or false (C#)

I had method, to check count of Landlords шт LandlordTypes我有方法,可以检查 Landlords шт LandlordTypes 的数量

I had this code to check it我有这个代码来检查它

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

if (type.Landlords.Count > 0)
{
    throw new UserFriendlyException(L("ThisTypeIsInUse"));
}

But I rewrote it like this但我是这样改写的

var type = _landlordTypeRepository
    .GetAll()
    .AnyAsync(x=> x.Id == input.Id && x.Landlords.Any());

But now return type id Task<bool>但是现在返回类型 id Task<bool>

How I can use this in if?我如何在 if 中使用它?

You need to use await :您需要使用await

var type = await _landlordTypeRepository.GetAll().AnyAsync(x=> x.Id == input.Id && x.Landlords.Any());

You method must also be marked as async .您的方法还必须标记为async

I recommend you get acquainted with async - await .我建议您熟悉async-await

Use await to "unwrap" Task result使用await来“解包” Task结果

bool type = await _landlordTypeRepository.GetAll().AnyAsync(x=> x.Id == input.Id && x.Landlords.Any());

Also mark calling method as async还将调用方法标记为async

async Task SomeMethod() {
    //..

    bool type = await _landlordTypeRepository.GetAll().AnyAsync(x=> x.Id == input.Id && x.Landlords.Any());

    //..
}

You can either use the await keyword or simply put .Result at the end of a task to get the value you are looking for.您可以使用await关键字或简单地将.Result放在任务的末尾以获得您正在寻找的值。

When using await , your method's signature must have async and must also return a Task (eg Task<string> or simply Task for void) unless it's an event which can then be async void .使用await ,您的方法的签名必须具有 async 并且还必须返回一个Task (例如Task<string>或简单的Task for void),除非它是一个可以是async void的事件。

Using .Result , will block the current thread which means can freeze the UI as well!使用.Result ,将阻塞当前线程,这意味着也可以冻结 UI!

A Task is a reference data structure used for asynchronous calls.任务是用于异步调用的参考数据结构。 It contains a .Result-property which will be filled with the return value once the call has completed.它包含一个 .Result 属性,一旦调用完成,它将用返回值填充。 Since it is an asynchronous call you cannot know for sure that the call has been completed when receiving a Task the way you have done here.由于它是一个异步调用,因此在以您在此处完成的方式接收任务时,您无法确定该调用是否已完成。

There is two main ways of dealing with this:有两种主要的处理方法:

  1. If you're inside an asynchronous context (method using the async keyword), you could just use the await -keyword to wait for and unwrap the result.如果您在异步上下文中(使用 async 关键字的方法),您可以只使用await关键字来等待并解包结果。 var type = await _landlordRepository.GetAll().AnyAsync(...);

  2. If you're in a synchronous context, you can use Task.Wait() to await the call and then read the result value from Task<T>.Result .如果您处于同步上下文中,则可以使用 Task.Wait() 等待调用,然后从Task<T>.Result .Result 读取结果值。

    var task = _landlordRepository.GetAll().AnyAsync(...).Wait(); var type = task.Result

As people have pointed out, using .Result is usually not advised since it is a blocking call.正如人们所指出的,通常不建议使用 .Result ,因为它是一个阻塞调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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