Re: Issue converting UInt32 to Int32 using Project()

39 views
Skip to first unread message

Jimmy Bogard

unread,
Jan 22, 2015, 12:37:02 PM1/22/15
to automapp...@googlegroups.com
Type converters aren't used in LINQ projections: 

Use ProjectUsing instead.

On Wed Jan 21 2015 at 8:01:33 PM CST <benjamin...@gmail.com> wrote:
Issue converting UInt32 to Int32 using Project()

Getting : 

An unhandled exception of type 'System.ArgumentException' occurred in System.Core.dll
Additional information: Argument types do not match

   at System.Linq.Expressions.Expression.Constant(Object value, Type type)
   at AutoMapper.QueryableExtensions.Impl.MappedTypeExpressionBinder.BindMappedTypeExpression(IMappingEngine mappingEngine, PropertyMap propertyMap, ExpressionRequest request, ExpressionResolutionResult result, IDictionary`2 typePairCount)
   at AutoMapper.QueryableExtensions.Impl.MappedTypeExpressionBinder.Build(IMappingEngine mappingEngine, PropertyMap propertyMap, TypeMap propertyTypeMap, ExpressionRequest request, ExpressionResolutionResult result, IDictionary`2 typePairCount)
   at AutoMapper.QueryableExtensions.Extensions.CreateMemberBindings(IMappingEngine mappingEngine, ExpressionRequest request, TypeMap typeMap, Expression instanceParameter, IDictionary`2 typePairCount)
   at AutoMapper.QueryableExtensions.Extensions.CreateMapExpression(IMappingEngine mappingEngine, ExpressionRequest request, Expression instanceParameter, IDictionary`2 typePairCount)
   at AutoMapper.QueryableExtensions.Extensions.CreateMapExpression(IMappingEngine mappingEngine, ExpressionRequest request, IDictionary`2 typePairCount)
   at AutoMapper.QueryableExtensions.Extensions.<>c__DisplayClass1`2.<CreateMapExpression>b__0(ExpressionRequest tp)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at AutoMapper.Internal.DictionaryFactoryOverride.ConcurrentDictionaryImpl`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at AutoMapper.QueryableExtensions.Extensions.CreateMapExpression[TSource,TDestination](IMappingEngine mappingEngine, IDictionary`2 parameters, String[] membersToExpand)
   at AutoMapper.QueryableExtensions.ProjectionExpression`1.To[TResult](IDictionary`2 parameters, String[] membersToExpand)
   at AutoMapper.QueryableExtensions.ProjectionExpression`1.To[TResult](Object parameters, String[] membersToExpand)
   at AutoMapper.QueryableExtensions.ProjectionExpression`1.To[TResult](Object parameters)
   at ConsoleApplication1.Program.Main(String[] args) in d:\Work\Proto\ConsoleApplication1\ConsoleApplication1\Program.cs:line 31
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

Is this a known issue ? Work Around would be appreciated.

Thanks,
-BEN

Source Code :


using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using AutoMapper.QueryableExtensions;
 
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Mapper.CreateMap<Object1Object2>()
                .ForMember(
                    dest => dest.Kimble,
                    opt => opt.MapFrom(source => source.Kimble));
            
            Mapper.CreateMap<Int32UInt32>().ConvertUsing<INT32UINT32Converter>();
 
            Mapper.AssertConfigurationIsValid();
 
            ICollection<Object1> objs1 = new Collection<Object1>()
                {
                    new Object1() { Name = "JustAName",  Kimble = 22 },
                    new Object1() { Name = "JustAName2"Kimble = 33 }
                };
 
            IQueryable newObj2 = objs1.AsQueryable().Project().To<Object2>();
        }
    }
 
    public class INT32UINT32Converter : ITypeConverter<intuint>
    {
        public uint Convert(ResolutionContext context)
        {
            return uint.Parse(context.SourceValue.ToString());
        }
    }
 
    public class Object1
    {
        public int Kimble { getset; }
 
        public string Name { getset; }
    }
 
 
    public class Object2
    {
        public uint Kimble { getset; }
 
        public string Name { getset; }
    }
}

--
You received this message because you are subscribed to the Google Groups "AutoMapper-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to automapper-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages