2016-06-15 3 views
1

このエラーがある質問はたくさんありますが、それはlambdaで発生する一般的なエラー多くのシナリオでは、しかし、私は自分の問題の原因を突き止めることはできません。私は怠け者を使用していますし、これが正常に動作します"lambda式をデリゲート型ではないため 'bool'型に変換することはできません" <T>

/// <summary> 
/// Build a client from the provided data entity. 
/// </summary> 
/// <param name="fromDataEntity">The data entity from which the client will be built.</param> 
/// <returns>The data entity that is built.</returns> 
private static Client BuildClient(ClientDataEntity fromDataEntity) 
{ 
    var client = new Client() 
    { 
     ClientCode = fromDataEntity.ClientCode, 
     Name = fromDataEntity.Name, 
     BillingAttorneyLazy = new Lazy<Employee>(() => EmployeeLoading.LoadEmployee(fromDataEntity.BillingAttorneyEmployeeUno)) 
    }; 

    return client; 
} 

は、ここだけの参考のためにEmployeeLoading.LoadEmployeeです:

/// <summary> 
/// Load the employee, if it exists, with the provided employee uno. 
/// </summary> 
/// <param name="withEmployeeUno">The employee uno for the employee that will be loaded.</param> 
/// <returns>The employee that is loaded, if one exists for the provided uno, or else null.</returns> 
internal static Employee LoadEmployee(uint withEmployeeUno) 
{ 
    var entity = CmsDataAbstraction.GetEmployeeDataEntity(withEmployeeUno); 
    return (entity != null) ? BuildEmployee(entity) : null; 
} 

、私は似たような行うときに:私は

/// <summary> 
/// Build and return an employee from the provided data entity. 
/// </summary> 
/// <param name="fromDataEntity">The data entity from which the employee will be built.</param> 
/// <returns>Build and return an employee from the provided data entity.</returns> 
private static Employee BuildEmployee(EmployeeDataEntity fromDataEntity) 
{ 
    var employee = new Employee() 
    { 
     EmployeeCode = fromDataEntity.Code, 
     WorksiteUserNameLazy = new Lazy<string>(() => GetEmployeeWorksiteUserName(employee)) 
    }; 

    return employee; 
} 

をラムダでエラーが発生する() => GetEmployeeWorksiteUserName(employee)

0それはここで

デリゲート型でないため

は、「ブール」を入力するラムダ式を変換できません参照のためGetEmployeeWorksiteUserName次のとおりです。

/// <summary> 
/// Retrieve the Worksite username for the provided employee. 
/// </summary> 
/// <param name="forEmployee">The employee whose Worksite username will be retrieved.</param> 
/// <returns>The Worksite username for the associated employee.</returns> 
private static string GetEmployeeWorksiteUserName(Employee forEmployee) 
{ 
    var authorADAccountName = FirmInformationDataAbstraction.GetEmployeeActiveDirectoryUsername(forEmployee.EmployeeCode); 

    if (WorksiteDataAbstraction.UserExists(authorADAccountName)) 
    { 
     return authorADAccountName; 
    } 
    else // user doesn't exist in Worksite. 
    { 
     return string.Empty; 
    } 
} 

私は、コンパイラは、私がしようとしていると考えて信じていますLazy<T>のコンストラクタを呼び出すには、boolという単一のメソッドが必要ですが、私のアプローチはうまくいくはずです(例えば、thisのようなサイトを参照してください)。

なぜこのアプローチは最初のケースでうまく動作し、2番目のケースでエラーが発生するのですか?どうすれば修正できますか?

+0

「WorksiteUserNameLazy」の種類は何ですか? – haim770

+0

'Lazy '。 –

+0

私はあなたが問題を強制して、デリゲートとbool 'new Lazyを受け取るコンストラクタを使うことができると思います。 – juharr

答えて

2

employeeが宣言される前に(コンストラクタで有効に)使用しているからです。

注違い:

var employee = new Employee() 
{ 
    EmployeeCode = fromDataEntity.Code 
}; 
employee.WorksiteUserNameLazy = 
    new Lazy<string>(() => GetEmployeeWorksiteUserName(employee)); 
1

私はあなたの問題を再現しようとしましたが、私はそれが宣言される前、従業員の参照を使用することはできませんだから、私はコンパイルエラーを取得しています。あなたの問題を解決するかどうかをチェックするために、この方法を試すことができますか?

/// <summary> 
/// Build and return an employee from the provided data entity. 
/// </summary> 
/// <param name="fromDataEntity">The data entity from which the employee will be built.</param> 
/// <returns>Build and return an employee from the provided data entity.</returns> 
private static Employee BuildEmployee(EmployeeDataEntity fromDataEntity) 
{ 
    var employee = new Employee() 
    { 
     EmployeeCode = fromDataEntity.Code    
    }; 
    employee.WorksiteUserNameLazy = new Lazy<string>(() => GetEmployeeWorksiteUserName(employee)); 
    return employee; 
} 
関連する問題