Problems returning a Iqueryable<> list to convert it with Automapper

Asked

Viewed 147 times

0

I have a function that returns me a list of people with their personal/legal relationships. I own a Personal Viewmodel that is already mapped in the Autommapper layer... When I call the Getjoinall() function it has to return me a People Iqueryable, only it is giving an error:

+		$exception	{System.Exception: Can't resolve this to Queryable Expression
   at AutoMapper.QueryableExtensions.ExpressionBuilder.ResolveExpression(PropertyMap propertyMap, Type currentType, Expression instanceParameter, LetPropertyMaps letPropertyMaps)
   at AutoMapper.QueryableExtensions.ExpressionBuilder.<CreateMemberBindings>g__CreateMemberBinding|15_0(PropertyMap propertyMap, <>c__DisplayClass15_0& )
   at AutoMapper.QueryableExtensions.ExpressionBuilder.CreateMemberBindings(ExpressionRequest request, TypeMap typeMap, Expression instanceParameter, IDictionary`2 typePairCount, LetPropertyMaps letPropertyMaps)
   at AutoMapper.QueryableExtensions.ExpressionBuilder.CreateMapExpressionCore(ExpressionRequest request, Expression instanceParameter, IDictionary`2 typePairCount, TypeMap typeMap, LetPropertyMaps letPropertyMaps)
   at AutoMapper.QueryableExtensions.ExpressionBuilder.CreateMapExpression(ExpressionRequest request, IDictionary`2 typePairCount, LetPropertyMaps letPropertyMaps)
   at AutoMapper.QueryableExtensions.Impl.EnumerableExpressionBinder.BindEnumerableExpression(IConfigurationProvider configuration, PropertyMap propertyMap, ExpressionRequest request, ExpressionResolutionResult result, IDictionary`2 typePairCount, LetPropertyMaps letPropertyMaps)
   at AutoMapper.QueryableExtensions.ExpressionBuilder.<CreateMemberBindings>g__CreateMemberBinding|15_0(PropertyMap propertyMap, <>c__DisplayClass15_0& )
   at AutoMapper.QueryableExtensions.ExpressionBuilder.CreateMemberBindings(ExpressionRequest request, TypeMap typeMap, Expression instanceParameter, IDictionary`2 typePairCount, LetPropertyMaps letPropertyMaps)
   at AutoMapper.QueryableExtensions.ExpressionBuilder.CreateMapExpressionCore(ExpressionRequest request, Expression instanceParameter, IDictionary`2 typePairCount, TypeMap typeMap, LetPropertyMaps letPropertyMaps)
   at AutoMapper.QueryableExtensions.ExpressionBuilder.CreateMapExpression(ExpressionRequest request, IDictionary`2 typePairCount, LetPropertyMaps letPropertyMaps)
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
   at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
   at System.Lazy`1.CreateValue()
   at AutoMapper.QueryableExtensions.ExpressionBuilder.GetMapExpression(Type sourceType, Type destinationType, IDictionary`2 parameters, MemberInfo[] membersToExpand)
   at AutoMapper.QueryableExtensions.ProjectionExpression.To[TResult](IDictionary`2 parameters, IEnumerable`1 memberPathsToExpand)
   at AutoMapper.QueryableExtensions.Extensions.ProjectTo[TDestination](IQueryable source, Expression`1[] membersToExpand)
   at SistemaComercial.Application.Services.PessoaAppService.GetJoinAll() in C:\Users\JALBER\Desktop\Projeto_Atual\SistemaComercial\src\SistemaComercial.Application\Services\PessoaAppService.cs:line 119
   at SistemaComercial.Presentation.Web.MVC.Controllers.PessoaController.Index() in C:\Users\JALBER\Desktop\Projeto_Atual\SistemaComercial\src\SistemaComercial.Presentation.Web.MVC\Controllers\PessoaController.cs:line 31
   at lambda_method(Closure , Object , Object[] )
   at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeActionMethodAsync>d__12.MoveNext()}	System.Exception

public class PessoaViewModel
    {
        [Key]
        [DisplayName("Código")]
        public int Id { get; set; }
        [DisplayName("Natureza")]
        [Required(ErrorMessage ="Escolha uma Natureza")]
        public PessoaNatureza PessoaNatureza { get; set; }
        [DisplayName("Natureza")]
        public string PessoaNaturezaDescricao { get; set; }
        [DisplayName("Naturezas")]
        public IEnumerable<SelectListItem> PessoasNaturezas { get; set; }

        public PessoaEntidadeViewModel PessoaEntidadeViewModel { get; set; }
        public IEnumerable<PessoaEntidadeViewModel> PessoasEntidades { get; set; }
        public IEnumerable<PessoaEntidadeViewModel> PessoasEntidadesFiliais { get; set; }

        public  PessoaFisicaViewModel PessoaFisicaViewModel { get; set; }
        public  PessoaJuridicaViewModel PessoaJuridicaViewModel { get; set; }

        public PessoaViewModel()
        {
            PessoasNaturezas = ExtensaoDeEnumerador.EnumParaSelectListGenerico<PessoaNatureza>("", PessoaNatureza.ToString()).OrderBy(x => x.Text);
            PessoaFisicaViewModel = null;
            PessoaJuridicaViewModel = null;
        }

 public IEnumerable<PessoaViewModel> GetJoinAll()
{
   return _pessoaRepository.GetJoinAll().ProjectTo<PessoaViewModel>();
}

 public IQueryable<Pessoa> GetJoinAll()
 {
    var pessoas = Db.Pessoa
    .Include(pe => pe.PessoasEntidades)
    .Include(pe => pe.PessoasEntidadesFiliais)
    .Include(pe => pe.PessoaJuridica)
    .Include(pe => pe.PessoaFisica)
    .ToList();
    
     return pessoas.AsQueryable();
 }

  //Mapeamento PessoaViewModel
            CreateMap<Pessoa, PessoaViewModel>()
                .ForMember(dest => dest.Id,
                    opt => opt.ResolveUsing(entity => entity.Id))
                .ForMember(dest => dest.PessoaNatureza,
                    opt => opt.ResolveUsing(entity => entity.PessoaNatureza))
                .ForMember(dest => dest.PessoaNaturezaDescricao,
                    opt => opt.ResolveUsing(entity => entity.PessoaNatureza.ObterDescricaoEnum()))
                .ForMember(dest => dest.PessoaFisicaViewModel,
                    opt => opt.ResolveUsing(entity => entity.PessoaFisica))
                .ForMember(dest => dest.PessoaJuridicaViewModel,
                    opt => opt.ResolveUsing(entity => entity.PessoaJuridica))
                .ForMember(dest => dest.PessoasEntidades,
                    opt => opt.ResolveUsing(entity => entity.PessoasEntidades))
                .ForMember(dest => dest.PessoasEntidadesFiliais,
                    opt => opt.ResolveUsing(entity => entity.PessoasEntidadesFiliais));

inserir a descrição da imagem aqui

Because it happens and how can I solve?

  • Just one observation: in your method GetJoinAll(), by forcing the query execution by calling ToList(), you lose the benefits of ProjectTo() offered by Automapper. The idea of ProjectTo is to project upon your IQueryable a query that only brings the required fields to the view model, but when using the ToList, you have already executed and brought all fields (including all classes in Includes).

  • About the problem, I would start trying to "comment" on the passages involving Enum.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.