0
votes

CreateMap

Mapper.CreateMap<Unidade, unidadeDTO>();
Mapper.CreateMap<unidadeDTO, Unidade>();




public ICollection<Unidade> BuscarPorParametos(Expression<Func<Unidade, bool>> parametros)
{
    return Mapper.Map<ICollection<unidadeDTO>, ICollection<Unidade>>(unidadeDeTrabalho.UnidadeDAO.BuscarPorParametros(Mapper.Map<Expression<Func<Unidade, bool>>, Expression<Func<unidadeDTO, bool>>>(parametros)));
}

Automapper exception:

Missing type map configuration or unsupported mapping.

Mapping types: Expression1 -> Expression1 System.Linq.Expressions.Expression1[[System.Func2[[Unidade, Dominio, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] -> System.Linq.Expressions.Expression1[[System.Func2[[unidadeDTO, Infraestrutura, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]

Destination path: Expression`1

Source value: p => ((p.CodUnidade == 1) AndAlso (p.CodTrabalho == 1))

1
I don't think you can use automapper on Expressions. You normally use the expressions as part of queries, then automap the results of those queries to different classes.Tim S.
Can you show the CreateMap statements?Gert Arnold
Mapper.CreateMap<Unidade, unidadeDTO>(); Mapper.CreateMap<unidadeDTO, Unidade>();joaoeduardorf
OK, so you want to map regular objects. What are you trying to achieve with the second Mapper.Map statement? Right now, you're trying to copy an expression to another expression, but you clearly don't intend to do that (and it's impossible).Gert Arnold
alright. i need mapping filter from repository to dao.joaoeduardorf

1 Answers

2
votes

When working with Expressions and AutoMapper you need to use the Queryable Extensions namespace.

It uses a different syntax than normal auto-mapper. Normally you would use it in line with your query to get the resultset you want.

//---- Declared elsewhere
Mapper.CreateMap<Unidade, unidadeDTO>();
Mapper.CreateMap<unidadeDTO, Unidade>();
//----

public static IQueryable<unidadeDTO> ConvertToDTO(IQueryable<Unidade> source)
{
    return source.Project().To<unidadeDTO>();
}

Now you can write filters expressions against the new IQueryable and the changes will be propagated back up to the original SQL. This allows you to do things like this

public class OrderLine
{
  public int Id { get; set; }
  public int OrderId { get; set; }
  public Item Item { get; set; }
  public decimal Quantity { get; set; }
}

public class Item
{
  public int Id { get; set; }
  public string Name { get; set; }
}

public class OrderLineDTO
{
  public int Id { get; set; }
  public int OrderId { get; set; }
  public string ItemName { get; set; }
  public decimal Quantity { get; set; }
}

public class OrderDAL
{
    static OrderDAL()
    {
        Mapper.CreateMap<OrderLine, OrderLineDTO>()
            .ForMember(dto => dto.ItemName, conf => conf.MapFrom(ol => ol.Item.Name);
    }

    public List<OrderLineDTO> GetLinesForOrder(string itemName)
    {
      using (var context = new orderEntities())
      {
        return context.OrderLines.Project().To<OrderLineDTO>()
               .Where(i => i.ItemName == itemName).ToList();
      }
    }
}

Notice how I am using the property ItemName of the DTO to perform the filtering.