alexvilla
alexvilla

Reputation: 23

Asp.Net Core Automapper LINQ expression

I'm currently working with ASP.NET Core and I want to use Automapper to map a Linq Expression. The mapping statement is:

var targetConditions = _mapper.Map<Expression<Func<Entity, bool>>>(filter);

where filter is a formal parameter in the form:

(Expression<Func<EntityDTO, bool>> filter

In the mapping profile I have the following map created:

CreateMap<Expression<Func<EntityDTO, bool>>, Expression<Func<Entity, bool>>>();

I'm using a generic repository pattern with EF. I want to get a list of DTOs filtered by, of course, DTO's fields from my controller. I then need to convert from DTO filter to entities filter in the Business Layer before doing any query using Linq for EF.

Even though the expression does get coverted from EntityDTO to Entity, the parameters in the lambda expressions inside don't, raising all sorts of errors when I further use it with EF. Any idea how can this be done?

Upvotes: 2

Views: 1510

Answers (1)

Yaser Moradi
Yaser Moradi

Reputation: 3307

Try this:

IQueryable<Customer> query = repository.getCustomers(); // entities query
query.ProjectTo<CustomerDto>().Where(dtoFilterExpression)

Upvotes: 2

Related Questions