Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QueryableExtensions: NullableExpressionBinder used in preference to CustomExpressionBinder #1879

Closed
roblugt opened this issue Jan 3, 2017 · 3 comments · Fixed by #1890
Closed
Labels
Milestone

Comments

@roblugt
Copy link

roblugt commented Jan 3, 2017

The following example demonstrates a problem where the NullableExpressionBinder is selected even though a custom ProjectUsing expression is configured. This results in an InvalidOperationException

No coercion operator is defined between types 'System.String' and 'System.Nullable`1[AutomapperEnumTest.Method]'.

using System;
using System.Linq;
using AutoMapper;
using AutoMapper.QueryableExtensions;

namespace AutomapperEnumTest
{
	public enum Method
	{
		MANUAL = 0,
		AUTO = 1
	}

	public class DestType
	{
		public string Name { get; set; }
		public Method? Method { get; set; }
	}

	public class SourceType
	{
		public string Name { get; set; }
		public string MethodName { get; set; }
	}

	public class Program
	{
		public static void Main()
		{
			Configure();
			var src = new SourceType[] { new SourceType { Name = "Rob", MethodName = "AUTO" } };
			var results = src.AsQueryable()
						.ProjectTo<DestType>();
		}

		private static void Configure()
		{
			Mapper.Initialize(cfg =>
			{
				cfg.CreateMap<string, Method?>().ProjectUsing(src => src == "MANUAL" ? Method.MANUAL : Method.AUTO);
				cfg.CreateMap<SourceType, DestType>()
					.ForMember(dest => dest.Method, opt => opt.MapFrom(src => src.MethodName));
			});
		}
	}
}

Perhaps the ExpressionBuilder class should change the order of Builders so that CustomProjectionExpressionBinder is listed first?

@lbargaoanu
Copy link
Member

Sounds reasonable. PR?

roblugt pushed a commit to roblugt/AutoMapper that referenced this issue Jan 5, 2017
@roblugt
Copy link
Author

roblugt commented Jan 5, 2017

Thanks PR #1890 created.

@jbogard jbogard added the Bug label Jan 5, 2017
@jbogard jbogard added this to the vNext milestone Jan 5, 2017
@lock
Copy link

lock bot commented May 7, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators May 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants