There are a lot of questions out there that have this error, but that's because it seems like a common error that occurs with lambdas in many scenarios; however, I can't pin down the reason for my issue.
I'm using Lazy and this works fine:
/// <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;
}
Here's EmployeeLoading.LoadEmployee
just for reference:
/// <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;
}
Now, when I do something similar:
/// <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;
}
I'm getting an error on the lambda () => GetEmployeeWorksiteUserName(employee)
:
Cannot convert lambda expression to type 'bool' because it is not a delegate type
Here is GetEmployeeWorksiteUserName
for reference:
/// <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;
}
}
I believe the compiler thinks I'm trying to call the constructor for Lazy<T>
which takes a single bool
, but it's well-documented that my approach should work (see sites like this, e.g.).
Why is this approach working fine in the first case and getting the error in the second case? How do I fix it?