2

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?

3
  • Did you have a map for CreateMap<EntityDTO, Entity>();? Commented Jun 29, 2017 at 19:15
  • @Win. Yes, I do have it Commented Jun 29, 2017 at 20:46
  • How do you use it? Could you create mcve? Commented Jun 29, 2017 at 21:36

1 Answer 1

2

Try this:

IQueryable<Customer> query = repository.getCustomers(); // entities query
query.ProjectTo<CustomerDto>().Where(dtoFilterExpression)
Sign up to request clarification or add additional context in comments.

3 Comments

That needs automapper ef extensions.
That needs IQueryableExtensions and works with any IQueryable, including entity framework ones. What's a problem here?
Thanks @YaserMoradi. That was it! I saw that solution before but I couldn't make it work for some reason. Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.