1

I have the following code

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");
        }

when invoked I get the following error

LINQ to Entities does not recognize the method RoleTypeEnum EntityRoleToDtoRole(User.Entities.Entities.Role)' method, and this method cannot be translated into a store expression.

How do I convert the EntityRoleToDtoRole to be callable from an Entity Framework query?

svick
  • 236,525
  • 50
  • 385
  • 514
John
  • 1,403
  • 3
  • 19
  • 31

1 Answers1

2

You need to use Users.AsEnumerable() to be able and call methods within 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();
Michael D. Irizarry
  • 6,186
  • 5
  • 30
  • 35