question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mapping a List to a larger List error

See original GitHub issue

To start, in a real run of my application this probably wouldn’t happen, but I wanted to understand why this is happening and if there is a workaround.

I have a mapping that is from a List of hourly weather data objects (forecast time, temperature, humidity, cloud cover, etc.). I am mapping to a List of SOAP message objects that can only handle one data type at a time. So I have to create a List of message objects for each weather attribute (temp, humidity, etc.). There can be multiple hours in each message, they just have to correspond to the same attribute. This creates for any size list an output list of 6 (# of weather attributes) message objects.

So, I could be mapping 2 hours of weather forecasts into 6 message objects, where each object contains 2 hours. Obviously, I am going to be getting around 24-48 hours of forecast data, so the source list is much larger than the 6 output. But when unit testing, I am only testing a few hours for quickness. This revealed an error in Automapper. It would appear Automapper makes the assumption that the destination list will be the same size or smaller than the source. Is there way to flag a mapping to not make this assumption?

Thank you for any assistance

Here is the error and the mapping code: Destination property: Capacity Exception of type ‘AutoMapper.AutoMapperMappingException’ was thrown. —> System.ArgumentOutOfRangeException: capacity was less than the current size. Parameter name: value

top of stack trace: System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource) System.Collections.Generic.List`1.set_Capacity(Int32 value) SetCapacity(Object , Object ) AutoMapper.Internal.PropertyAccessor.SetValue(Object destination, Object value)

// Don’t worry about syntax, this is a very cut down version of my mapping, but this covers the logic I explained up top // Also, there is no issue with my mapping logic because I pull out everything inside the construct using and run directly in the unit test it works perfect. My assumption is automapper assumes the list is going to get smaller Mapper.CreateMap<List<WeatherInstance>, PowerSystemDataType>() .ConstructUsing(src => { var psds = new PowerSystemDataType(); // collection object of type List<PSD> psds.Add(new PSD(Type=“TempF”, Value=src.Temperature)); psds.Add(new PSD(Type=“Humidity”, Value=src.Humidity)); psds.Add(new PSD(Type=“Cloud Cover”, Value=src.CloudCover)); … more weather attributes return psds; });

Issue Analytics

  • State:closed
  • Created 11 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
mick-wilsoncommented, Jan 10, 2013

I’m guessing based on this:

For this example, obviously the source is 6 items, but the mapping should create a List of PSDs that has a size of 18 (6 attributes * 3 distinct stations).

…that an earlier part of the mapping or some internal mechanism of PowerSystemDataType is setting the Count of the destination list to 18. Subsequently, when AutoMapper reaches the Capacity property, it’s trying to ratchet it back down to 6, which is illegal:

http://msdn.microsoft.com/en-us/library/y52x03h2.aspx (see Exceptions)

Have you considered

.ForMember(b => b.Capacity, o => o.UseDestinationValue());

… which ignores the (invalid) source capacity when writing the destination object?

0reactions
lock[bot]commented, May 5, 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Getting an error while trying to map a list of objects
Map function from the Stream API, is expecting a function that return a value. <R> Stream<R> map(Function<? super T,? extends R> mapper).
Read more >
How to avoid "Can't find list" for large lists?
I can see their List names at https://www.google.com/maps/ under Menu -> Your places -> Lists, but when I try to open one (like...
Read more >
Apply a function to items of a list with map() in Python
In Python, you can use map() to apply built-in functions, lambda expressions (lambda), functions defined with def, etc., to all items of ...
Read more >
Python's map(): Processing Iterables Without a Loop
Python's map() is a built-in function that allows you to process and transform all the items in an iterable without using an explicit...
Read more >
Apply a function to list-elements of a list — lmap
A function that takes a length-1 list and returns a list (of any length.) ... Additional arguments passed on to the mapped function....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found