1

我有以下代码

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)' 方法,并且此方法无法转换为存储表达式。

如何将EntityRoleToDtoRoleEntity Framework 查询转换为可调用的?

4

1 回答 1

2

您需要使用 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();
于 2013-04-10T13:52:39.197 回答