简体   繁体   English

我如何在从实体框架模型中检索的linq查询中调用方法

[英]How do I call a method from within a linq query that is retrieving from an Entity Framework model

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. LINQ to Entities无法识别RoleTypeEnum EntityRoleToDtoRole(User.Entities.Entities.Role)'方法,并且该方法无法转换为商店表达式。

How do I convert the EntityRoleToDtoRole to be callable from an Entity Framework query? 如何将EntityRoleToDtoRole转换为可从Entity Framework查询中调用?

You need to use Users.AsEnumerable() to be able and call methods within linq. 您需要使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM