Asif Aziz

Asif Aziz

  • 1.7k
  • 74
  • 6.1k

Conversion of Expression in Automapper

Feb 13 2023 9:47 AM

Hi, I am working on an applicaition in which I am communicating Database via Generic Repository and UnitOfWork. In my service layer i have to convert my viewmodels to actual database models ,with the help of automapper, in order to get data from database. All is going well but I having issue when want to convert exression by automapper. It is converted but when in this method

public IQueryable<TEntity> GetWithInclude(System.Linq.Expressions.Expression<Func<TEntity, bool>> predicate, params string[] include)
        {
            IQueryable<TEntity> query = this.DbSet;
            query = include.Aggregate(query, (current, inc) => current.Include(inc));
            var result= query.Where(predicate).ToList();
            return query.Where(predicate);
        }  t
this statement:
 var result= query.Where(predicate).ToList();
try to execute it gives this error.

System.ArgumentException: 'ParameterExpression of type 'BusinessEntities.ProductEntity' cannot be used for delegate parameter of type 'DataModel.Data.tblProduct''

here is debug view after conversion 
.Lambda #Lambda1<System.Func`2[DataModel.Data.tblProduct,System.Boolean]>(BusinessEntities.ProductEntity $x) {
    $x.Price > (System.Nullable`1[System.Decimal])((System.Decimal)0)
}

this is my mapping code 


            MapperConfiguration _config = new MapperConfiguration(cfg => cfg.CreateMap<Expression<Func<ProductEntity, bool>> , Expression<Func<tblProduct, bool>>> ());
            IMapper _Mapper = _config.CreateMapper();
            var exp = _Mapper.Map<Expression<Func<tblProduct, bool>>>(query); 

How can I change delegate parameter to type tblProduct.

Thanks in advance.

Regards,

Asif Aziz.


Answers (2)