[英]How do I call a method from within a linq query that is retrieving from an Entity Framework model
我有以下代码
return (_entities.Users.Select(profile => new ProfileUserListItemDto
{
Email = profile.Email,
FirstName = profile.FirstName,
Id = profile.Id,
LastName = profile.LastName,
Role = DtoEntityLookups.EntityRoleToDtoRole(profile.Role),
TimeZone = profile.TimeZone
})).ToList();
public static RoleTypeEnum EntityRoleToDtoRole(Role role)
{
if (role == null)
throw new NoNullAllowedException("Null role supplied to EntityRoleToDtoRole method");
if (role.Id.ToString() == RolesGuid.AdministratorGuid)
return RoleTypeEnum.Administrator;
if (role.Id.ToString() == RolesGuid.ClientGuid)
return RoleTypeEnum.Client;
throw new InvalidDataException("Unknown role supplied");
}
调用时,出现以下错误
LINQ to Entities无法识别RoleTypeEnum EntityRoleToDtoRole(User.Entities.Entities.Role)'方法,并且该方法无法转换为商店表达式。
如何将EntityRoleToDtoRole
转换为可从Entity Framework查询中调用?
您需要使用Users.AsEnumerable()才能在linq中调用方法。
return (_entities.Users.AsEnumerable().Select(profile => new ProfileUserListItemDto
{
Email = profile.Email,
FirstName = profile.FirstName,
Id = profile.Id,
LastName = profile.LastName,
Role = DtoEntityLookups.EntityRoleToDtoRole(profile.Role),
TimeZone = profile.TimeZone
})).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.