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

IEnumerable is enumerating twice when adapted to array #272

Closed
DaniilPalii opened this issue Oct 6, 2020 · 4 comments
Closed

IEnumerable is enumerating twice when adapted to array #272

DaniilPalii opened this issue Oct 6, 2020 · 4 comments

Comments

@DaniilPalii
Copy link

DaniilPalii commented Oct 6, 2020

When adapting IEnumerable to an array, it's adapted twice.

Example of code:

        static void Main(string[] args)
        {
            GetMyObjects().Adapt<MyObject[]>();

            Console.WriteLine(EnumerateTimes);
        }

        static IEnumerable<MyObject> GetMyObjects()
        {
            EnumerateTimes++;
            yield return new MyObject { SomeProperty = "A" };
            yield return new MyObject { SomeProperty = "B" };
        }

        static int EnumerateTimes = 0;

Result of executing: 2.
Expected result: 1.

Version: 6.5.0.

@DaniilPalii
Copy link
Author

I assume that proper workaround for now is calling ToList before adapting:

GetMyObjects().ToList().Adapt<MyObject[]>();

@DaniilPalii DaniilPalii changed the title IEnumerable is enumerating twice when adaptet to array IEnumerable is enumerating twice when adapted to array Oct 6, 2020
@chaowlert
Copy link
Collaborator

Thanks for reporting. Problem with array is we need to know array size before creating array, so it create extra enumerable access because enumerable has no Count method. I will check how to solve this problem.

@satano
Copy link
Member

satano commented Oct 9, 2020

Possible solution is to create a List<T> and then call ToArray(). Of course, data (references in case of objects) are doubled.

@DaniilPalii
Copy link
Author

Thank You!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants