2013-05-08 48 views
0

我有這樣的方法:變換私人判斷方法,以異步方式

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

我想這個方法來異步返回,即Task<Predicate<int>>。 我該如何去做這件事? 目前,我在async(id)上有編輯錯誤。

+1

這只是返回一個謂詞 - 你確定它是你想異步運行的GetQueryFilter方法,而不是謂詞本身?如果你想要一個異步謂詞,那是另一回事。如果你能在這裏解釋最終目的,那真的很有幫助。 – 2013-05-08 20:51:11

回答

1

你在問什麼沒有什麼意義。

Task<Predicate<int>>是一個返回謂詞的異步方法。

你要做的是編寫一個異步行爲的謂詞。換句話說,Func<int, Task<bool>>將是一個異步謂詞。

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

但實際異步謂詞可能不會對任何代碼調用此工作。您必須確定處理該問題的最佳方法。