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

Converting a large list to a connection. #19

Open
MartinDawson opened this issue Mar 30, 2018 · 2 comments
Open

Converting a large list to a connection. #19

MartinDawson opened this issue Mar 30, 2018 · 2 comments
Labels
question Further information is requested

Comments

@MartinDawson
Copy link
Contributor

MartinDawson commented Mar 30, 2018

Hey,

So I used to do this to create my connection:

Connection<LiveStreamPayload>()
    .Name("liveStreams")
    .Resolve(c =>
    {
        var liveStreams = liveStreamService.GetAudios();
        var splitLiveStreams = ConnectionUtils.ToConection(liveStreams);

        return splitLiveStreams;
    });

However the code internally calls .ToList() which means that for every page it is returning all of my 25k records from the database and it would be incredibly slow obviously.

So instead of doing this I now do:

Connection<LiveStreamPayload>()
    .Name("liveStreams")
    .Resolve(c =>
    {
        var offset = ConnectionUtils.OffsetOrDefault(c.After, 0);
        var liveStreams = liveStreamService.GetAudios();
        var newLiveStreams = liveStreams.Take(c.First.Value + (offset + 1));
        var count = liveStreamService.GetCount();
        var splitLiveStreams = ConnectionUtils.ToConnection(newLiveStreams, c, 0, count);

        return splitLiveStreams;
    });

public IQueryable<LiveStream> GetAudios() { return _repository.GetAll(); }

So this doesn't load the entire 25k records anymore, only the data I need.

Is this the correct way to do this or am I doing something wrong? It was very hard to figure this out and I feel it may be the wrong way.

I'm using Entity Framework core.

Thanks.

@furier
Copy link

furier commented Apr 12, 2019

There should be a ConnectionUtils.ToConection method for IQueryable, applying the paging arguments before passing it to EntityFrameworkCore to prevent putting potentially thousands or millions of records into memory.

@sungam3r sungam3r added the question Further information is requested label Nov 3, 2019
@myty
Copy link
Contributor

myty commented Nov 11, 2021

There should be a ConnectionUtils.ToConection method for IQueryable, applying the paging arguments before passing it to EntityFrameworkCore to prevent putting potentially thousands or millions of records into memory.

I'm working on this very thing. It's for IEnumerable but should theoretically work for IQueryable. I'm thinking this project could use an EFCore sample project to really highlight its usefulness. The EnumerableSliceMetrics class still needs a little more cleanup and some Benchmarks to see what the performance implications are between EnumerableSliceMetrics and ArraySliceMetrics.

I may have gotten a little carried away with updating the Star Wars sample project, but it reads so nice!

I suppose I could open a PR at this point and clean up a little more, but check it out: master...myty:feature/enumerable-connections

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

No branches or pull requests

4 participants