2015-05-13 67 views
0
public IEnumerable<CustomBo> FindBy(Expression<Func<CustomBo, bool>> predicate) 
    { 
     Mapper.CreateMap<Expression<Func<CustomBo, bool>>, Expression<Func<Entity, bool>>>(); 

     var newPredicate = Mapper.Map<Expression<Func<Entity, bool>>>(predicate); 

     IQueryable<Entity> query = dbSet.Where(newPredicate); 

     Mapper.CreateMap<Entity,CustomBo>(); 

     var searchResult = Mapper.Map<List<CustomBo>>(query); 

     return searchResult; 
    } 

我要地圖customBo類型實體類型..自動映射:如何映射表達式

這裏customBo是我的模型和實體從EDMX數據庫實體。

我正在使用AutoMapper。

我收到以下錯誤

無法找到目標類型Data.Customer源型Model.CustomerBO類型的地圖。使用CreateMap創建從源到目標類型的映射。 找不到從目標類型Data.Customer到源類型Model.CustomerBO的類型映射。使用CreateMap創建從源到目標類型的映射。

任何Suggession什麼我missiong這裏..

感謝

回答

0

我發現周圍的工作。我創建了我的自定義方法來映射表達式。

public static class MappingHelper 
{ 
    public static Expression<Func<TTo, bool>> ConvertExpression<TFrom, TTo>(this Expression<Func<TFrom, bool>> expr) 
    { 
     Dictionary<Expression, Expression> substitutues = new Dictionary<Expression, Expression>(); 
     var oldParam = expr.Parameters[0]; 
     var newParam = Expression.Parameter(typeof(TTo), oldParam.Name); 
     substitutues.Add(oldParam, newParam); 
     Expression body = ConvertNode(expr.Body, substitutues); 
     return Expression.Lambda<Func<TTo, bool>>(body, newParam); 
    } 

    static Expression ConvertNode(Expression node, IDictionary<Expression, Expression> subst) 
    { 
     if (node == null) return null; 
     if (subst.ContainsKey(node)) return subst[node]; 

     switch (node.NodeType) 
     { 
      case ExpressionType.Constant: 
       return node; 
      case ExpressionType.MemberAccess: 
       { 
        var me = (MemberExpression)node; 
        var newNode = ConvertNode(me.Expression, subst); 

        MemberInfo info = null; 
        foreach (MemberInfo mi in newNode.Type.GetMembers()) 
        { 
         if (mi.MemberType == MemberTypes.Property) 
         { 
          if (mi.Name.ToLower().Contains(me.Member.Name.ToLower())) 
          { 
           info = mi; 
           break; 
          } 
         } 
        } 
        return Expression.MakeMemberAccess(newNode, info); 
       } 
      case ExpressionType.AndAlso: 
      case ExpressionType.OrElse: 
      case ExpressionType.LessThan: 
      case ExpressionType.LessThanOrEqual: 
      case ExpressionType.GreaterThan: 
      case ExpressionType.GreaterThanOrEqual: 
      case ExpressionType.Equal: /* will probably work for a range of common binary-expressions */ 
       { 
        var be = (BinaryExpression)node; 
        return Expression.MakeBinary(be.NodeType, ConvertNode(be.Left, subst), ConvertNode(be.Right, subst), be.IsLiftedToNull, be.Method); 
       } 

      default: 
       throw new NotSupportedException(node.NodeType.ToString()); 
     } 
    } 
} 

現在,我稱它像

public CustomBo FindBy(Expression<Func<CustomBo, bool>> predicateId) 
    { 
     var newPredicate = predicateId.ConvertExpression<CustomBo, Entity>(); 
    } 

不過,如果有人知道如何automapper做那麼PLZ讓我知道。

感謝