![]() |
VOOZH | about |
dotnet add package AutoMapper.Extensions.ExpressionMapping --version 11.0.0
NuGet\Install-Package AutoMapper.Extensions.ExpressionMapping -Version 11.0.0
<PackageReference Include="AutoMapper.Extensions.ExpressionMapping" Version="11.0.0" />
<PackageVersion Include="AutoMapper.Extensions.ExpressionMapping" Version="11.0.0" />Directory.Packages.props
<PackageReference Include="AutoMapper.Extensions.ExpressionMapping" />Project file
paket add AutoMapper.Extensions.ExpressionMapping --version 11.0.0
#r "nuget: AutoMapper.Extensions.ExpressionMapping, 11.0.0"
#:package AutoMapper.Extensions.ExpressionMapping@11.0.0
#addin nuget:?package=AutoMapper.Extensions.ExpressionMapping&version=11.0.0Install as a Cake Addin
#tool nuget:?package=AutoMapper.Extensions.ExpressionMapping&version=11.0.0Install as a Cake Tool
AutoMapper extentions for mapping expressions (OData)
To use, configure using the configuration helper method:
var mapper = new Mapper(new MapperConfiguration(cfg => {
cfg.AddExpressionMapping();
// Rest of your configuration
}, loggerFactory));
// or if using the MS Ext DI:
services.AddAutoMapper(cfg => {
cfg.AddExpressionMapping();
}, /* assemblies with profiles */);
Expression Mapping also supports writing queries against the mapped objects. Take the following source and destination types:
public class User
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Request
{
public int Id { get; set; }
public int AssigneeId { get; set; }
public User Assignee { get; set; }
}
public class UserDTO
{
public int Id { get; set; }
public string Name { get; set; }
}
public class RequestDTO
{
public int Id { get; set; }
public UserDTO Assignee { get; set; }
}
We can write LINQ expressions against the DTO collections.
ICollection<RequestDTO> requests = [.. context.Request.GetQuery1<RequestDTO, Request>(mapper, r => r.Id > 0 && r.Id < 3, null, [r => r.Assignee])];
ICollection <UserDTO> users = [.. context.User.GetQuery1<UserDTO, User>(mapper, u => u.Id > 0 && u.Id < 4, q => q.OrderBy(u => u.Name))];
int count = await context.Request.Query<RequestDTO, Request, int, int>(mapper, q => q.Count(r => r.Id > 1));
The methods below map the DTO query expresions to the equivalent data query expressions. The call to IMapper.Map converts the data query results back to the DTO (or model) object types. The call to IMapper.ProjectTo converts the data query to a DTO (or model) query.
static class Extensions
{
internal static async Task<TModelResult> Query<TModel, TData, TModelResult, TDataResult>(this IQueryable<TData> query, IMapper mapper,
Expression<Func<IQueryable<TModel>, TModelResult>> queryFunc) where TData : class
{
//Map the expressions
Func<IQueryable<TData>, TDataResult> mappedQueryFunc = mapper.MapExpression<Expression<Func<IQueryable<TData>, TDataResult>>>(queryFunc).Compile();
//execute the query
return mapper.Map<TDataResult, TModelResult>(mappedQueryFunc(query));
}
//This example compiles the queryable expression.
internal static IQueryable<TModel> GetQuery1<TModel, TData>(this IQueryable<TData> query,
IMapper mapper,
Expression<Func<TModel, bool>> filter = null,
Expression<Func<IQueryable<TModel>, IQueryable<TModel>>> queryableExpression = null,
IEnumerable<Expression<Func<TModel, object>>> expansions = null)
{
Func<IQueryable<TData>, IQueryable<TData>> mappedQueryDelegate = mapper.MapExpression<Expression<Func<IQueryable<TData>, IQueryable<TData>>>>(queryableExpression)?.Compile();
if (filter != null)
query = query.Where(mapper.MapExpression<Expression<Func<TData, bool>>>(filter));
return mappedQueryDelegate != null
? mapper.ProjectTo(mappedQueryDelegate(query), null, GetExpansions())
: mapper.ProjectTo(query, null, GetExpansions());
Expression<Func<TModel, object>>[] GetExpansions() => expansions?.ToArray() ?? [];
}
//This example updates IQueryable<TData>.Expression with the mapped queryable expression argument.
internal static IQueryable<TModel> GetQuery2<TModel, TData>(this IQueryable<TData> query,
IMapper mapper,
Expression<Func<TModel, bool>> filter = null,
Expression<Func<IQueryable<TModel>, IQueryable<TModel>>> queryableExpression = null,
IEnumerable<Expression<Func<TModel, object>>> expansions = null)
{
Expression<Func<IQueryable<TData>, IQueryable<TData>>> mappedQueryExpression = mapper.MapExpression<Expression<Func<IQueryable<TData>, IQueryable<TData>>>>(queryableExpression);
if (filter != null)
query = query.Where(mapper.MapExpression<Expression<Func<TData, bool>>>(filter));
if (mappedQueryExpression != null)
{
var queryableExpressionBody = GetUnconvertedExpression(mappedQueryExpression.Body);
queryableExpressionBody = ReplaceParameter(queryableExpressionBody, mappedQueryExpression.Parameters[0], query.Expression);
query = query.Provider.CreateQuery<TData>(queryableExpressionBody);
}
return mapper.ProjectTo(query, null, GetExpansions());
Expression<Func<TModel, object>>[] GetExpansions() => expansions?.ToArray() ?? [];
static Expression GetUnconvertedExpression(Expression expression) => expression.NodeType switch
{
ExpressionType.Convert or ExpressionType.ConvertChecked or ExpressionType.TypeAs => GetUnconvertedExpression(((UnaryExpression)expression).Operand),
_ => expression,
};
Expression ReplaceParameter(Expression expression, ParameterExpression source, Expression target) => new ParameterReplacer(source, target).Visit(expression);
}
class ParameterReplacer(ParameterExpression source, Expression target) : ExpressionVisitor
{
private readonly ParameterExpression _source = source;
private readonly Expression _target = target;
protected override Expression VisitParameter(ParameterExpression node)
{
return node == _source ? _target : base.VisitParameter(node);
}
}
}
Mapping a single type in the source expression to multiple types in the destination expression is not supported e.g.
[Fact]
public void Can_map_if_source_type_targets_multiple_destination_types_in_the_same_expression()
{
var mapper = ConfigurationHelper.GetMapperConfiguration(cfg =>
{
cfg.CreateMap<SourceType, TargetType>().ReverseMap();
cfg.CreateMap<SourceChildType, TargetChildType>().ReverseMap();
// Same source type can map to different target types. This seems unsupported currently.
cfg.CreateMap<SourceListItemType, TargetListItemType>().ReverseMap();
cfg.CreateMap<SourceListItemType, TargetChildListItemType>().ReverseMap();
}).CreateMapper();
Expression<Func<SourceType, bool>> sourcesWithListItemsExpr = src => src.Id != 0 && src.ItemList.Any() && src.Child.ItemList.Any(); // Sources with non-empty ItemList
Expression<Func<TargetType, bool>> target1sWithListItemsExpr = mapper.MapExpression<Expression<Func<TargetType, bool>>>(sourcesWithListItemsExpr);
}
private class SourceChildType
{
public int Id { get; set; }
public IEnumerable<SourceListItemType> ItemList { get; set; } // Uses same type (SourceListItemType) for its itemlist as SourceType
}
private class SourceType
{
public int Id { get; set; }
public SourceChildType Child { set; get; }
public IEnumerable<SourceListItemType> ItemList { get; set; }
}
private class SourceListItemType
{
public int Id { get; set; }
}
private class TargetChildType
{
public virtual int Id { get; set; }
public virtual ICollection<TargetChildListItemType> ItemList { get; set; } = [];
}
private class TargetChildListItemType
{
public virtual int Id { get; set; }
}
private class TargetType
{
public virtual int Id { get; set; }
public virtual TargetChildType Child { get; set; }
public virtual ICollection<TargetListItemType> ItemList { get; set; } = [];
}
private class TargetListItemType
{
public virtual int Id { get; set; }
}
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 is compatible. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 is compatible. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 was computed. |
| .NET Framework | net461 net461 was computed. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
Showing the top 5 NuGet packages that depend on AutoMapper.Extensions.ExpressionMapping:
| Package | Downloads |
|---|---|
|
AutoMapper.Collection.EntityFrameworkCore
Collection updating support for EntityFrameworkCore with AutoMapper. Extends DBSet<T> with Persist<TDto>().InsertUpdate(dto) and Persist<TDto>().Delete(dto). Will find the matching object and will Insert/Update/Delete. |
|
|
AutoMapper.AspNetCore.OData.EFCore
Creates LINQ expressions from ODataQueryOptions and executes the query. |
|
|
AutoMapper.Collection.EntityFramework
Collection updating support for EntityFramework with AutoMapper. Extends DBSet<T> with Persist<TDto>().InsertUpdate(dto) and Persist<TDto>().Delete(dto). Will find the matching object and will Insert/Update/Delete. |
|
|
Envoc.Core.Queries.Datatables
Package Description |
|
|
Fluent.Architecture.Core
Package Description |
Showing the top 5 popular GitHub repositories that depend on AutoMapper.Extensions.ExpressionMapping:
| Repository | Stars |
|---|---|
|
revoframework/Revo
Event Sourcing, CQRS and DDD framework for C#/.NET Core.
|
|
|
AutoMapper/AutoMapper.Collection
AutoMapper support for updating existing collections by equivalency
|
|
|
AutoMapper/AutoMapper.Collection.EFCore
EFCore support for AutoMapper.Collections
|
|
|
AutoMapper/AutoMapper.Extensions.OData
Creates LINQ expressions from ODataQueryOptions and executes the query.
|
|
|
Kation/ComBoost
ComBoost是一个领域驱动的快速开发框架
|
| Version | Downloads | Last Updated |
|---|---|---|
| 11.0.0 | 114,392 | 3/23/2026 |
| 10.1.0 | 48,382 | 2/19/2026 |
| 10.0.0 | 87,865 | 12/10/2025 |
| 9.0.1 | 75,038 | 7/14/2025 |
| 9.0.0 | 73,516 | 7/12/2025 |
| 8.0.0 | 1,514,477 | 2/15/2025 |
| 7.0.2 | 1,980,561 | 9/16/2024 |
| 7.0.1 | 4,852,178 | 5/15/2024 |
| 7.0.0 | 3,013,082 | 2/7/2024 |
| 6.0.4 | 1,919,095 | 4/2/2023 |
| 6.0.3 | 299,466 | 1/27/2023 |
| 6.0.2 | 440,522 | 11/26/2022 |
| 6.0.1 | 166,315 | 10/30/2022 |
| 6.0.0 | 1,539,274 | 10/1/2022 |
| 5.1.0 | 702,853 | 6/28/2022 |
| 5.0.3 | 254,328 | 6/16/2022 |
| 5.0.2 | 266,115 | 4/8/2022 |
| 5.0.1 | 57,760 | 2/26/2022 |
| 5.0.0 | 766,109 | 1/5/2022 |
| 4.1.5 | 283,298 | 3/21/2022 |
Removing obsolete methods and classes. Making AutoMapper v16.1.1 the minimum supported version.