"Cannot convert lambda expression to type 'bool' because it is not a delegate type" 懒惰<T>
"Cannot convert lambda expression to type 'bool' because it is not a delegate type" with Lazy<T>
有很多问题都存在此错误,但那是因为在许多情况下,这似乎是 lambda 出现的常见错误;但是,我无法确定问题的原因。
我正在使用 Lazy,这很好用:
/// <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;
}
我在 lambda 上遇到错误 () => GetEmployeeWorksiteUserName(employee)
:
Cannot convert lambda expression to type 'bool' because it is not a
delegate type
这里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, 例如).
为什么这种方法在第一种情况下工作正常,而在第二种情况下却出现错误?我该如何解决?
这是因为您在 employee
声明之前使用它(在构造函数中有效)。
注意区别:
var employee = new Employee()
{
EmployeeCode = fromDataEntity.Code
};
employee.WorksiteUserNameLazy =
new Lazy<string>(() => GetEmployeeWorksiteUserName(employee));
我已尝试重现您的问题,但出现编译器错误,因为我无法在声明之前使用员工引用。您可以尝试这样做以检查它是否解决了您的问题?
/// <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;
}
有很多问题都存在此错误,但那是因为在许多情况下,这似乎是 lambda 出现的常见错误;但是,我无法确定问题的原因。
我正在使用 Lazy,这很好用:
/// <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;
}
我在 lambda 上遇到错误 () => GetEmployeeWorksiteUserName(employee)
:
Cannot convert lambda expression to type 'bool' because it is not a delegate type
这里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, 例如).
为什么这种方法在第一种情况下工作正常,而在第二种情况下却出现错误?我该如何解决?
这是因为您在 employee
声明之前使用它(在构造函数中有效)。
注意区别:
var employee = new Employee()
{
EmployeeCode = fromDataEntity.Code
};
employee.WorksiteUserNameLazy =
new Lazy<string>(() => GetEmployeeWorksiteUserName(employee));
我已尝试重现您的问题,但出现编译器错误,因为我无法在声明之前使用员工引用。您可以尝试这样做以检查它是否解决了您的问题?
/// <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;
}