List remove range by passing list as argument





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-1















I have a list of missing items like this and I'm trying to perform a bulk operation against my DB table by taking out small chunks of items from the existing list (2500 items at a time):



 var castedItems = missingItems.ToList();
while (castedItems.Any())
{
var subList = castedItems.Take(2500).ToList();
DBRetry.Do(() => EFBatchOperation.For(ctx,
ctx.SearchedUserItems).InsertAll(subList), TimeSpan.FromSeconds(2));
castedItems.RemoveRange(subList);
}


Missing items is a conccurrent bag , while casted items are list. Now I should create an extension method which would accept the list that I took out and remove those 2500 items that were inserted into the database quickly... But I'm not quite sure how to do that ...



Can someone help me out?



P.S. The problematic part is this one:



castedItems.RemoveRange(subList);           


It says that removerange only accepts two integers as arguments ( from and to ) :/










share|improve this question

























  • did you try passing the offset and limit? like 0 and 2500 first time?

    – Akbar Badhusha
    Jan 4 at 14:41











  • something like this castedItems.Except(subList).ToList();

    – Milind Anantwar
    Jan 4 at 14:42













  • @MilindAnantwar do you mean like: castedItems= castedItems.Except(subList).ToList(); ?

    – User987
    Jan 4 at 14:44













  • @AkbarBadhusha Can you show me an example ?

    – User987
    Jan 4 at 14:44






  • 1





    If you want to be removing items from the front of the collection you really ought to be using a Queue, not a list. These operations are some of the most inefficient operations you can perform on a list. A queue is specifically designed to do exactly this type of thing efficiently. That's assuming you need to put the items into a new collection at all, rather than just iterating missingItems and never putting the items into a materialized question to begin with (which would be ideal if feasible).

    – Servy
    Jan 4 at 14:47




















-1















I have a list of missing items like this and I'm trying to perform a bulk operation against my DB table by taking out small chunks of items from the existing list (2500 items at a time):



 var castedItems = missingItems.ToList();
while (castedItems.Any())
{
var subList = castedItems.Take(2500).ToList();
DBRetry.Do(() => EFBatchOperation.For(ctx,
ctx.SearchedUserItems).InsertAll(subList), TimeSpan.FromSeconds(2));
castedItems.RemoveRange(subList);
}


Missing items is a conccurrent bag , while casted items are list. Now I should create an extension method which would accept the list that I took out and remove those 2500 items that were inserted into the database quickly... But I'm not quite sure how to do that ...



Can someone help me out?



P.S. The problematic part is this one:



castedItems.RemoveRange(subList);           


It says that removerange only accepts two integers as arguments ( from and to ) :/










share|improve this question

























  • did you try passing the offset and limit? like 0 and 2500 first time?

    – Akbar Badhusha
    Jan 4 at 14:41











  • something like this castedItems.Except(subList).ToList();

    – Milind Anantwar
    Jan 4 at 14:42













  • @MilindAnantwar do you mean like: castedItems= castedItems.Except(subList).ToList(); ?

    – User987
    Jan 4 at 14:44













  • @AkbarBadhusha Can you show me an example ?

    – User987
    Jan 4 at 14:44






  • 1





    If you want to be removing items from the front of the collection you really ought to be using a Queue, not a list. These operations are some of the most inefficient operations you can perform on a list. A queue is specifically designed to do exactly this type of thing efficiently. That's assuming you need to put the items into a new collection at all, rather than just iterating missingItems and never putting the items into a materialized question to begin with (which would be ideal if feasible).

    – Servy
    Jan 4 at 14:47
















-1












-1








-1








I have a list of missing items like this and I'm trying to perform a bulk operation against my DB table by taking out small chunks of items from the existing list (2500 items at a time):



 var castedItems = missingItems.ToList();
while (castedItems.Any())
{
var subList = castedItems.Take(2500).ToList();
DBRetry.Do(() => EFBatchOperation.For(ctx,
ctx.SearchedUserItems).InsertAll(subList), TimeSpan.FromSeconds(2));
castedItems.RemoveRange(subList);
}


Missing items is a conccurrent bag , while casted items are list. Now I should create an extension method which would accept the list that I took out and remove those 2500 items that were inserted into the database quickly... But I'm not quite sure how to do that ...



Can someone help me out?



P.S. The problematic part is this one:



castedItems.RemoveRange(subList);           


It says that removerange only accepts two integers as arguments ( from and to ) :/










share|improve this question
















I have a list of missing items like this and I'm trying to perform a bulk operation against my DB table by taking out small chunks of items from the existing list (2500 items at a time):



 var castedItems = missingItems.ToList();
while (castedItems.Any())
{
var subList = castedItems.Take(2500).ToList();
DBRetry.Do(() => EFBatchOperation.For(ctx,
ctx.SearchedUserItems).InsertAll(subList), TimeSpan.FromSeconds(2));
castedItems.RemoveRange(subList);
}


Missing items is a conccurrent bag , while casted items are list. Now I should create an extension method which would accept the list that I took out and remove those 2500 items that were inserted into the database quickly... But I'm not quite sure how to do that ...



Can someone help me out?



P.S. The problematic part is this one:



castedItems.RemoveRange(subList);           


It says that removerange only accepts two integers as arguments ( from and to ) :/







c# linq c#-4.0 extension-methods






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 4 at 14:45









Aristos

59.1k12101137




59.1k12101137










asked Jan 4 at 14:38









User987User987

1,40732149




1,40732149













  • did you try passing the offset and limit? like 0 and 2500 first time?

    – Akbar Badhusha
    Jan 4 at 14:41











  • something like this castedItems.Except(subList).ToList();

    – Milind Anantwar
    Jan 4 at 14:42













  • @MilindAnantwar do you mean like: castedItems= castedItems.Except(subList).ToList(); ?

    – User987
    Jan 4 at 14:44













  • @AkbarBadhusha Can you show me an example ?

    – User987
    Jan 4 at 14:44






  • 1





    If you want to be removing items from the front of the collection you really ought to be using a Queue, not a list. These operations are some of the most inefficient operations you can perform on a list. A queue is specifically designed to do exactly this type of thing efficiently. That's assuming you need to put the items into a new collection at all, rather than just iterating missingItems and never putting the items into a materialized question to begin with (which would be ideal if feasible).

    – Servy
    Jan 4 at 14:47





















  • did you try passing the offset and limit? like 0 and 2500 first time?

    – Akbar Badhusha
    Jan 4 at 14:41











  • something like this castedItems.Except(subList).ToList();

    – Milind Anantwar
    Jan 4 at 14:42













  • @MilindAnantwar do you mean like: castedItems= castedItems.Except(subList).ToList(); ?

    – User987
    Jan 4 at 14:44













  • @AkbarBadhusha Can you show me an example ?

    – User987
    Jan 4 at 14:44






  • 1





    If you want to be removing items from the front of the collection you really ought to be using a Queue, not a list. These operations are some of the most inefficient operations you can perform on a list. A queue is specifically designed to do exactly this type of thing efficiently. That's assuming you need to put the items into a new collection at all, rather than just iterating missingItems and never putting the items into a materialized question to begin with (which would be ideal if feasible).

    – Servy
    Jan 4 at 14:47



















did you try passing the offset and limit? like 0 and 2500 first time?

– Akbar Badhusha
Jan 4 at 14:41





did you try passing the offset and limit? like 0 and 2500 first time?

– Akbar Badhusha
Jan 4 at 14:41













something like this castedItems.Except(subList).ToList();

– Milind Anantwar
Jan 4 at 14:42







something like this castedItems.Except(subList).ToList();

– Milind Anantwar
Jan 4 at 14:42















@MilindAnantwar do you mean like: castedItems= castedItems.Except(subList).ToList(); ?

– User987
Jan 4 at 14:44







@MilindAnantwar do you mean like: castedItems= castedItems.Except(subList).ToList(); ?

– User987
Jan 4 at 14:44















@AkbarBadhusha Can you show me an example ?

– User987
Jan 4 at 14:44





@AkbarBadhusha Can you show me an example ?

– User987
Jan 4 at 14:44




1




1





If you want to be removing items from the front of the collection you really ought to be using a Queue, not a list. These operations are some of the most inefficient operations you can perform on a list. A queue is specifically designed to do exactly this type of thing efficiently. That's assuming you need to put the items into a new collection at all, rather than just iterating missingItems and never putting the items into a materialized question to begin with (which would be ideal if feasible).

– Servy
Jan 4 at 14:47







If you want to be removing items from the front of the collection you really ought to be using a Queue, not a list. These operations are some of the most inefficient operations you can perform on a list. A queue is specifically designed to do exactly this type of thing efficiently. That's assuming you need to put the items into a new collection at all, rather than just iterating missingItems and never putting the items into a materialized question to begin with (which would be ideal if feasible).

– Servy
Jan 4 at 14:47














3 Answers
3






active

oldest

votes


















0














I believe you can simply do:



castedItems.RemoveRange(0,2500);





share|improve this answer































    0














    var castedItems = missingItems.ToList();
    var offset = 0;
    var limit = 0;
    while (castedItems.Any())
    {
    var subList = castedItems.Take(2500).ToList();
    limit = limit + 2500;
    DBRetry.Do(() => EFBatchOperation.For(ctx,
    ctx.SearchedUserItems).InsertAll(subList), TimeSpan.FromSeconds(2));
    castedItems.RemoveRange(offset, limit);
    offset = offset + limit?
    }


    Try this, offset limit updating might need to updated. Didn't check the working






    share|improve this answer































      0














      Assuming you have a good compairer you can do Except.



      castedItems = castedItems.Except(subList);


      see https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.except?view=netframework-4.7.2



      That is the general case. In your case you can use skip:



      var castedItems = castedItems.Skip(2500);


      Skip is the "reverse" of take.



      Skip in this case is also much faster than RemoveRange or Except for two reasons, no allocation is needed and it can be lazy.






      share|improve this answer


























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54041049%2flist-remove-range-by-passing-list-as-argument%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0














        I believe you can simply do:



        castedItems.RemoveRange(0,2500);





        share|improve this answer




























          0














          I believe you can simply do:



          castedItems.RemoveRange(0,2500);





          share|improve this answer


























            0












            0








            0







            I believe you can simply do:



            castedItems.RemoveRange(0,2500);





            share|improve this answer













            I believe you can simply do:



            castedItems.RemoveRange(0,2500);






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 4 at 14:51









            apomeneapomene

            11.1k93559




            11.1k93559

























                0














                var castedItems = missingItems.ToList();
                var offset = 0;
                var limit = 0;
                while (castedItems.Any())
                {
                var subList = castedItems.Take(2500).ToList();
                limit = limit + 2500;
                DBRetry.Do(() => EFBatchOperation.For(ctx,
                ctx.SearchedUserItems).InsertAll(subList), TimeSpan.FromSeconds(2));
                castedItems.RemoveRange(offset, limit);
                offset = offset + limit?
                }


                Try this, offset limit updating might need to updated. Didn't check the working






                share|improve this answer




























                  0














                  var castedItems = missingItems.ToList();
                  var offset = 0;
                  var limit = 0;
                  while (castedItems.Any())
                  {
                  var subList = castedItems.Take(2500).ToList();
                  limit = limit + 2500;
                  DBRetry.Do(() => EFBatchOperation.For(ctx,
                  ctx.SearchedUserItems).InsertAll(subList), TimeSpan.FromSeconds(2));
                  castedItems.RemoveRange(offset, limit);
                  offset = offset + limit?
                  }


                  Try this, offset limit updating might need to updated. Didn't check the working






                  share|improve this answer


























                    0












                    0








                    0







                    var castedItems = missingItems.ToList();
                    var offset = 0;
                    var limit = 0;
                    while (castedItems.Any())
                    {
                    var subList = castedItems.Take(2500).ToList();
                    limit = limit + 2500;
                    DBRetry.Do(() => EFBatchOperation.For(ctx,
                    ctx.SearchedUserItems).InsertAll(subList), TimeSpan.FromSeconds(2));
                    castedItems.RemoveRange(offset, limit);
                    offset = offset + limit?
                    }


                    Try this, offset limit updating might need to updated. Didn't check the working






                    share|improve this answer













                    var castedItems = missingItems.ToList();
                    var offset = 0;
                    var limit = 0;
                    while (castedItems.Any())
                    {
                    var subList = castedItems.Take(2500).ToList();
                    limit = limit + 2500;
                    DBRetry.Do(() => EFBatchOperation.For(ctx,
                    ctx.SearchedUserItems).InsertAll(subList), TimeSpan.FromSeconds(2));
                    castedItems.RemoveRange(offset, limit);
                    offset = offset + limit?
                    }


                    Try this, offset limit updating might need to updated. Didn't check the working







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 4 at 14:45









                    Akbar BadhushaAkbar Badhusha

                    869918




                    869918























                        0














                        Assuming you have a good compairer you can do Except.



                        castedItems = castedItems.Except(subList);


                        see https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.except?view=netframework-4.7.2



                        That is the general case. In your case you can use skip:



                        var castedItems = castedItems.Skip(2500);


                        Skip is the "reverse" of take.



                        Skip in this case is also much faster than RemoveRange or Except for two reasons, no allocation is needed and it can be lazy.






                        share|improve this answer






























                          0














                          Assuming you have a good compairer you can do Except.



                          castedItems = castedItems.Except(subList);


                          see https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.except?view=netframework-4.7.2



                          That is the general case. In your case you can use skip:



                          var castedItems = castedItems.Skip(2500);


                          Skip is the "reverse" of take.



                          Skip in this case is also much faster than RemoveRange or Except for two reasons, no allocation is needed and it can be lazy.






                          share|improve this answer




























                            0












                            0








                            0







                            Assuming you have a good compairer you can do Except.



                            castedItems = castedItems.Except(subList);


                            see https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.except?view=netframework-4.7.2



                            That is the general case. In your case you can use skip:



                            var castedItems = castedItems.Skip(2500);


                            Skip is the "reverse" of take.



                            Skip in this case is also much faster than RemoveRange or Except for two reasons, no allocation is needed and it can be lazy.






                            share|improve this answer















                            Assuming you have a good compairer you can do Except.



                            castedItems = castedItems.Except(subList);


                            see https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.except?view=netframework-4.7.2



                            That is the general case. In your case you can use skip:



                            var castedItems = castedItems.Skip(2500);


                            Skip is the "reverse" of take.



                            Skip in this case is also much faster than RemoveRange or Except for two reasons, no allocation is needed and it can be lazy.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Jan 4 at 14:59

























                            answered Jan 4 at 14:54









                            HoganHogan

                            55.8k967103




                            55.8k967103






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54041049%2flist-remove-range-by-passing-list-as-argument%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Monofisismo

                                Angular Downloading a file using contenturl with Basic Authentication

                                Olmecas