// 配置 AutoMapper
Mapper.CreateMap<Order, OrderDto>();
// 执行 mapping
OrderDto dto = Mapper.Map<Order, OrderDto>(order);
using (var context = new OrderContext())
{
var orderConsignee = from order in context.Orders
select new OrderConsignee
{
OrderConsigneeId = order.OrderId,
//OrderItems = order.OrderItems,
OrderItemCount = order.OrderItemCount,
ConsigneeName = order.ConsigneeName,
ConsigneeRealName = order.ConsigneeRealName,
ConsigneePhone = order.ConsigneePhone,
ConsigneeProvince = order.ConsigneeProvince,
ConsigneeAddress = order.ConsigneeAddress,
ConsigneeZip = order.ConsigneeZip,
ConsigneeTel = order.ConsigneeTel,
ConsigneeFax = order.ConsigneeFax,
ConsigneeEmail = order.ConsigneeEmail
Console.ReadKey();
}
SELECT
[Extent1].[OrderItemCount] AS [OrderItemCount],
[Extent1].[OrderId] AS [OrderId],
[Extent1].[ConsigneeName] AS [ConsigneeName],
[Extent1].[ConsigneeRealName] AS [ConsigneeRealName],
[Extent1].[ConsigneePhone] AS [ConsigneePhone],
[Extent1].[ConsigneeProvince] AS [ConsigneeProvince],
[Extent1].[ConsigneeAddress] AS [ConsigneeAddress],
[Extent1].[ConsigneeZip] AS [ConsigneeZip],
[Extent1].[ConsigneeTel] AS [ConsigneeTel],
[Extent1].[ConsigneeFax] AS [ConsigneeFax],
[Extent1].[ConsigneeEmail] AS [ConsigneeEmail]
FROM [dbo].[Orders] AS [Extent1]
OrderDto dto = Mapper.Map<Order, OrderDto>(order);相处的问题?
Mapper.CreateMap<Order, OrderConsignee>();
var details = Mapper.Map<IEnumerable<Order>, IEnumerable<OrderConsignee>>(context.Orders).ToList();
SELECT
[Extent1].[OrderId] AS [OrderId],
[Extent1].[OrderItemCount] AS [OrderItemCount],
[Extent1].[UserId] AS [UserId],
[Extent1].[ReceiverId] AS [ReceiverId],
[Extent1].[ShopDate] AS [ShopDate],
[Extent1].[OrderDate] AS [OrderDate],
[Extent1].[ConsigneeRealName] AS [ConsigneeRealName],
[Extent1].[ConsigneeName] AS [ConsigneeName],
[Extent1].[ConsigneePhone] AS [ConsigneePhone],
[Extent1].[ConsigneeProvince] AS [ConsigneeProvince],
[Extent1].[ConsigneeAddress] AS [ConsigneeAddress],
[Extent1].[ConsigneeZip] AS [ConsigneeZip],
[Extent1].[ConsigneeTel] AS [ConsigneeTel],
[Extent1].[ConsigneeFax] AS [ConsigneeFax],
[Extent1].[ConsigneeEmail] AS [ConsigneeEmail],
[Extent1].[WhetherCouAndinte] AS [WhetherCouAndinte],
[Extent1].[ParvalueAndInte] AS [ParvalueAndInte],
[Extent1].[PaymentType] AS [PaymentType],
[Extent1].[Payment] AS [Payment],
[Extent1].[Courier] AS [Courier],
[Extent1].[TotalPrice] AS [TotalPrice],
[Extent1].[FactPrice] AS [FactPrice],
[Extent1].[Invoice] AS [Invoice],
[Extent1].[Remark] AS [Remark],
[Extent1].[OrderStatus] AS [OrderStatus],
[Extent1].[SaleUserID] AS [SaleUserID],
[Extent1].[SaleUserType] AS [SaleUserType],
[Extent1].[BusinessmanID] AS [BusinessmanID],
[Extent1].[Carriage] AS [Carriage],
[Extent1].[PaymentStatus] AS [PaymentStatus],
[Extent1].[OgisticsStatus] AS [OgisticsStatus],
[Extent1].[OrderType] AS [OrderType],
[Extent1].[IsOrderNormal] AS [IsOrderNormal]
FROM [dbo].[Orders] AS [Extent1]
public static class QueryableExtensions
{
public static ProjectionExpression<TSource> Project<TSource>(this IQueryable<TSource> source)
{
return new ProjectionExpression<TSource>(source);
}
}
public class ProjectionExpression<TSource>
{
private static readonly Dictionary<string, Expression> ExpressionCache = new Dictionary<string, Expression>();
private readonly IQueryable<TSource> _source;
public ProjectionExpression(IQueryable<TSource> source)
{
_source = source;
}
public IQueryable<TDest> To<TDest>()
{
var queryExpression = GetCachedExpression<TDest>() ?? BuildExpression<TDest>();
return _source.Select(queryExpression);
}
private static Expression<Func<TSource, TDest>> GetCachedExpression<TDest>()
{
var key = GetCacheKey<TDest>();
return ExpressionCache.ContainsKey(key) ? ExpressionCache[key] as Expression<Func<TSource, TDest>> : null;
}
private static Expression<Func<TSource, TDest>> BuildExpression<TDest>()
{
var sourceProperties = typeof(TSource).GetProperties();
var destinationProperties = typeof(TDest).GetProperties().Where(dest => dest.CanWrite);
var parameterExpression = Expression.Parameter(typeof(TSource), "src");
var bindings = destinationProperties
.Select(destinationProperty => BuildBinding(parameterExpression, destinationProperty, sourceProperties))
.Where(binding => binding != null);
var expression = Expression.Lambda<Func<TSource, TDest>>(Expression.MemberInit(Expression.New(typeof(TDest)), bindings), parameterExpression);
var key = GetCacheKey<TDest>();
ExpressionCache.Add(key, expression);
return expression;
}
private static MemberAssignment BuildBinding(Expression parameterExpression, MemberInfo destinationProperty, IEnumerable<PropertyInfo> sourceProperties)
{
var sourceProperty = sourceProperties.FirstOrDefault(src => src.Name == destinationProperty.Name);
if (sourceProperty != null)
{
return Expression.Bind(destinationProperty, Expression.Property(parameterExpression, sourceProperty));
}
var propertyNames = SplitCamelCase(destinationProperty.Name);
)
{
sourceProperty = sourceProperties.FirstOrDefault(src => src.Name == propertyNames[]);
if (sourceProperty != null)
{
]);
if (sourceChildProperty != null)
{
return Expression.Bind(destinationProperty, Expression.Property(Expression.Property(parameterExpression, sourceProperty), sourceChildProperty));
}
}
}
return null;
}
private static string GetCacheKey<TDest>()
{
return string.Concat(typeof(TSource).FullName, typeof(TDest).FullName);
}
private static string[] SplitCamelCase(string input)
{
return Regex.Replace(input, "([A-Z])", " $1", RegexOptions.Compiled).Trim().Split();
}
}
Mapper.CreateMap<Order, OrderConsignee>();
var details = context.Orders.Project().To<OrderConsignee>();
SELECT
[Project1].[OrderId] AS [OrderId],
[Project1].[OrderItemCount] AS [OrderItemCount],
[Project1].[ConsigneeRealName] AS [ConsigneeRealName],
[Project1].[ConsigneeName] AS [ConsigneeName],
[Project1].[ConsigneePhone] AS [ConsigneePhone],
[Project1].[ConsigneeProvince] AS [ConsigneeProvince],
[Project1].[ConsigneeAddress] AS [ConsigneeAddress],
[Project1].[ConsigneeZip] AS [ConsigneeZip],
[Project1].[ConsigneeTel] AS [ConsigneeTel],
[Project1].[ConsigneeFax] AS [ConsigneeFax],
[Project1].[ConsigneeEmail] AS [ConsigneeEmail],
[Project1].[C1] AS [C1],
[Project1].[OrderItemId] AS [OrderItemId],
[Project1].[ProName] AS [ProName],
[Project1].[ProImg] AS [ProImg],
[Project1].[ProPrice] AS [ProPrice],
[Project1].[ProNum] AS [ProNum],
[Project1].[AddTime] AS [AddTime],
[Project1].[ProOtherPara] AS [ProOtherPara],
[Project1].[Order_OrderId] AS [Order_OrderId]
FROM ( SELECT
[Extent1].[OrderId] AS [OrderId],
[Extent1].[OrderItemCount] AS [OrderItemCount],
[Extent1].[ConsigneeRealName] AS [ConsigneeRealName],
[Extent1].[ConsigneeName] AS [ConsigneeName],
[Extent1].[ConsigneePhone] AS [ConsigneePhone],
[Extent1].[ConsigneeProvince] AS [ConsigneeProvince],
[Extent1].[ConsigneeAddress] AS [ConsigneeAddress],
[Extent1].[ConsigneeZip] AS [ConsigneeZip],
[Extent1].[ConsigneeTel] AS [ConsigneeTel],
[Extent1].[ConsigneeFax] AS [ConsigneeFax],
[Extent1].[ConsigneeEmail] AS [ConsigneeEmail],
[Extent2].[OrderItemId] AS [OrderItemId],
[Extent2].[ProName] AS [ProName],
[Extent2].[ProImg] AS [ProImg],
[Extent2].[ProPrice] AS [ProPrice],
[Extent2].[ProNum] AS [ProNum],
[Extent2].[AddTime] AS [AddTime],
[Extent2].[ProOtherPara] AS [ProOtherPara],
[Extent2].[Order_OrderId] AS [Order_OrderId],
END AS [C1]
FROM [dbo].[Orders] AS [Extent1]
LEFT OUTER JOIN [dbo].[OrderItems] AS [Extent2] ON [Extent1].[OrderId] = [Extent2].[Order_OrderId]
) AS [Project1]
ORDER BY [Project1].[OrderId] ASC, [Project1].[C1] ASC
/// <summary>
/// 订单项
/// </summary>
public virtual ICollection<OrderItem> OrderItems { get; set; }
本文为 @ 21CTO 创作并授权 21CTO 发布,未经许可,请勿转载。
内容授权事宜请您联系 webmaster@21cto.com或关注 21CTO 公众号。
该文观点仅代表作者本人,21CTO 平台仅提供信息存储空间服务。