Question

I have this method:

/// <summary>
/// Gets the query filter.
/// </summary>
/// <param name="queryText">The query text.</param>
/// <returns>The query filter predicate.</returns>
private Task<Predicate<int>> GetQueryFilter(string queryText)
{
    // Return the query filter predicate
    return new Predicate<int>(async(id) =>
    {
        // Get the employee
        StructuredEmployee employee = await LoadEmployee(id);
        // If employee not found - return false
        if (employee == null)
            return false;
        // Else if employee is found
        else
            // Check subject and body
            return (!string.IsNullOrWhiteSpace(employee.FirstName)) && employee.FirstName.Contains(queryText)
                || (!string.IsNullOrWhiteSpace(employee.MiddleName)) && employee.MiddleName.Contains(queryText)
                || (!string.IsNullOrWhiteSpace(employee.LastName)) && employee.LastName.Contains(queryText);
    });
}

I want this method to return asynchronously, i.e. Task<Predicate<int>>. How do I go about doing this? Currently I have a compilation error on async(id).

Était-ce utile?

La solution

What you're asking doesn't make much sense.

Task<Predicate<int>> is an asynchronous method that returns a predicate.

What you're trying to do is write a predicate that acts asynchronously. In other words, Func<int, Task<bool>> would be an asynchronous predicate.

private Func<int, Task<bool>> GetQueryFilter(string queryText)
{
  return new Func<int, Task<bool>>(async (id) =>
  {
    ...
  };
}

But an actual asynchronous predicate probably won't work well for whatever code is calling this. You'll have to determine what the best way is to handle that.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top