AutoMapper.Extensions.ExpressionMapping 11.0.0

Prefix Reserved
dotnet add package AutoMapper.Extensions.ExpressionMapping --version 11.0.0
                    
NuGet\Install-Package AutoMapper.Extensions.ExpressionMapping -Version 11.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AutoMapper.Extensions.ExpressionMapping" Version="11.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AutoMapper.Extensions.ExpressionMapping" Version="11.0.0" />
                    
Directory.Packages.props
<PackageReference Include="AutoMapper.Extensions.ExpressionMapping" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add AutoMapper.Extensions.ExpressionMapping --version 11.0.0
                    
#r "nuget: AutoMapper.Extensions.ExpressionMapping, 11.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package AutoMapper.Extensions.ExpressionMapping@11.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=AutoMapper.Extensions.ExpressionMapping&version=11.0.0
                    
Install as a Cake Addin
#tool nuget:?package=AutoMapper.Extensions.ExpressionMapping&version=11.0.0
                    
Install as a Cake Tool

OData

AutoMapper extentions for mapping expressions (OData)

CI CodeQL NuGet

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 */);

DTO Queries

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);
            }
        }
    }

Known Issues

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 Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (74)

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

GitHub repositories (5)

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 105 3/23/2026
10.1.0 9,311 2/19/2026
10.0.0 33,020 12/10/2025
9.0.1 57,392 7/14/2025
9.0.0 40,400 7/12/2025
8.0.0 1,180,174 2/15/2025
7.0.2 1,741,781 9/16/2024
7.0.1 4,068,854 5/15/2024
7.0.0 2,780,476 2/7/2024
6.0.4 1,795,416 4/2/2023
6.0.3 294,515 1/27/2023
6.0.2 425,251 11/26/2022
6.0.1 163,754 10/30/2022
6.0.0 1,448,590 10/1/2022
5.1.0 691,166 6/28/2022
5.0.3 253,550 6/16/2022
5.0.2 264,511 4/8/2022
5.0.1 57,542 2/26/2022
5.0.0 746,861 1/5/2022
4.1.5 263,767 3/21/2022
Loading failed

Removing obsolete methods and classes. Making AutoMapper v16.1.1 the minimum supported version.