AutoMapper / AutoMapper.Extensions.OData

Creates LINQ expressions from ODataQueryOptions and executes the query.
MIT License
140 stars 38 forks source link

Not working filter operator in on enums #184

Closed petrkasnal closed 1 year ago

petrkasnal commented 1 year ago

Hello,

last week i create issue #183 for not working filter "in" on enums. I create for it repository. If you run it try call this url and you will see full error. Thank you very much https://localhost:7000/Customer?$filter=CustomerType in ('New', 'External')

https://github.com/petrkasnal/ODataFilterEnumExample/tree/main

System.InvalidOperationException: 'No generic method 'Contains' on type 'System.Linq.Enumerable' is compatible with the supplied type arguments and arguments. No type arguments should be provided if the method is non-generic. '

BlaiseD commented 1 year ago

It looks like OData is giving you a list without nullable Enums - you're checking to see if it contains a nullable (possible compilation error I think). You'll need something like the following:

    public class CustomerDto
    {
        public int Id { get; set; }
        public string? Name { get; set; }
        public string? Description { get; set; }
        public CustomerTypeDto CustomerType { get; set; }//Not Nullable
    }
    public class Customer
    {
        public int Id { get; set; }
        public string? Name { get; set; }    
        public string? Description { get; set; }
        public CustomerType? CustomerType { get; set; }
    }

var config = new MapperConfiguration(cfg =>
{
    //Configuring Employee and EmployeeDTO
    cfg.CreateMap<Customer, CustomerDto>()
        .ForMember(dest => dest.CustomerType, opts => opts.MapFrom(src => (CustomerTypeDto)(src.CustomerType.HasValue ? (int)src.CustomerType.Value : -1)));
    cfg.CreateMap<CustomerType, CustomerTypeDto>();
    //Any Other Mapping Configuration ....
});

And this in your project: <PackageReference Include="AutoMapper.Extensions.ExpressionMapping" Version="6.0.4" /> The latest version of this library references 6.0.0.

BlaiseD commented 1 year ago

I don't think you need the CreateMap between Enums either.