3

Trying to use IQueryable extensoins of Automapper.
The followng code:

class Program
{
    public class A { public string[] Strings { get; set; } }
    public class B { public string[] Lines { get; set; } }

    static void Main(string[] args)
    {
        Mapper.CreateMap<A, B>()
              .ForMember(m => m.Lines, action => action.MapFrom(src => src.Strings));

        IQueryable<A> qa = (new List<A>() {new A() {Strings = new string[0]}}).AsQueryable();
        IQueryable<B> b = qa.Project().To<B>(); // error is here
    }
}

gives error "Sequence contains no elements". This is because this line:

          .ForMember(m => m.Lines, action => action.MapFrom(src => src.Strings));

What am i doing wrong? I know there is a simplier solution for this particular case but i meet this trouble everytime i use collections in ".MapFrom" method.
STack trace says:

at System.Linq.Enumerable.First[TSource](IEnumerable1 source)
at AutoMapper.MappingEngine.CreateMapExpression(Type typeIn, Type typeOut) at AutoMapper.MappingEngine.<CreateMapExpression>b__9[TSource,TDestination](TypePair tp) at System.Collections.Concurrent.ConcurrentDictionary
2.GetOrAdd(TKey key, Func2 valueFactory) at AutoMapper.MappingEngine.CreateMapExpression[TSource,TDestination]()
at ConsoleApplication1.ProjectionExpression
1.ToTResult in d:\Work\Misc\VS\ConsoleApplication1\ConsoleApplication1\Program.cs:line 57 at ConsoleApplication1.Program.Main(String[] args) in d:\Work\Misc\VS\ConsoleApplication1\ConsoleApplication1\Program.cs:line 21 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()

so it performs First() at some point. Why? How to solve it?
Thanks.

4

0 回答 0