LINQ and AutoMapper

前端 未结 1 768
借酒劲吻你
借酒劲吻你 2020-12-30 04:19

I\'m wondering if there is anything I can do to get this working as expected. This works fine in my code:

        var roles = _securityContext.Set

        
相关标签:
1条回答
  • 2020-12-30 04:28

    You're operating on IQueryable, which tries to translate the method you supply into SQL. Obviously, it's not going to happen, as you're passing AutoMapper related magic. There's an easy solution, you have to force execution before mapping:

    return roles.ToList().Select(role => Mapper.Map<Role, RoleDto>(role)).ToList();
    

    [EDIT] As Daniel suggests in comments, you can always go from IQueryable to IEnumerable by calling AsEnumerable() and still defer execution:

    return roles.AsEnumerable().Select(role => Mapper.Map<Role, RoleDto>(role)).ToList();
    
    0 讨论(0)
提交回复
热议问题