Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WCF and WF
  4. Insert mutiple items in ObservableCollection<t></t>

Insert mutiple items in ObservableCollection<t></t>

Scheduled Pinned Locked Moved WCF and WF
designquestion
11 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 3 3fonov

    Is it possible to insert multiple items in ObservableCollection? I have about 5000 items and inserting it one by one causing UI to go very slow. Thanks.

    T Offline
    T Offline
    Timmy Kokke
    wrote on last edited by
    #2

    How are they stored at the moment? in a List? You can convert a List to an ObservableCollection thru its constructor: ObservableCollection<object> Col = new ObservableCollection<object>(originalList); You can concatenate one sequence to another with: Collection1 = Collection1.Concat(Collection2); Hope this helps.

    Dawn is nature's way of telling you to go to bed.

    3 1 Reply Last reply
    0
    • T Timmy Kokke

      How are they stored at the moment? in a List? You can convert a List to an ObservableCollection thru its constructor: ObservableCollection<object> Col = new ObservableCollection<object>(originalList); You can concatenate one sequence to another with: Collection1 = Collection1.Concat(Collection2); Hope this helps.

      Dawn is nature's way of telling you to go to bed.

      3 Offline
      3 Offline
      3fonov
      wrote on last edited by
      #3

      Thanks a lot. First one is good enough at system startup when a lot of items received but unusable during runtime. Second soluction return IEnumerable. So i need to use ctor after that. This not i'm actually want. If system running and i'll recreate object collection i'm also need to recreate all bindings to it. It's seems imposssible :-)

      1 Reply Last reply
      0
      • 3 3fonov

        Is it possible to insert multiple items in ObservableCollection? I have about 5000 items and inserting it one by one causing UI to go very slow. Thanks.

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #4

        It's not a great idea to attempt to add bulk items into an ObservableCollection. That's not what it's designed to do - and will go slow because it raises a CollectionChanged event on every add. One option would be to derive a custom observable collection which suppresses this event until you have finished adding the data. Off the top of my head, this should work:

        public class SuperObservableCollection<T> : ObservableCollection<T>
        {
        private bool _suppressNotification = false;

        protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
        if (!_suppressNotification)
        base.OnCollectionChanged(e);
        }

        public void AddRange(IEnumerable<T> list)
        {
        _suppressNotification = true;

        foreach (T item in list)
        {
          Add(item);
        }
        \_suppressNotification = false;
        OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
        

        }
        }

        Deja View - the feeling that you've seen this post before.

        My blog | My articles | MoXAML PowerToys

        R 3 2 Replies Last reply
        0
        • P Pete OHanlon

          It's not a great idea to attempt to add bulk items into an ObservableCollection. That's not what it's designed to do - and will go slow because it raises a CollectionChanged event on every add. One option would be to derive a custom observable collection which suppresses this event until you have finished adding the data. Off the top of my head, this should work:

          public class SuperObservableCollection<T> : ObservableCollection<T>
          {
          private bool _suppressNotification = false;

          protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
          {
          if (!_suppressNotification)
          base.OnCollectionChanged(e);
          }

          public void AddRange(IEnumerable<T> list)
          {
          _suppressNotification = true;

          foreach (T item in list)
          {
            Add(item);
          }
          \_suppressNotification = false;
          OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
          

          }
          }

          Deja View - the feeling that you've seen this post before.

          My blog | My articles | MoXAML PowerToys

          R Offline
          R Offline
          RugbyLeague
          wrote on last edited by
          #5

          Works for me - thanks

          P 1 Reply Last reply
          0
          • R RugbyLeague

            Works for me - thanks

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #6

            Cool, and no problem.

            Deja View - the feeling that you've seen this post before.

            My blog | My articles | MoXAML PowerToys

            R 1 Reply Last reply
            0
            • P Pete OHanlon

              Cool, and no problem.

              Deja View - the feeling that you've seen this post before.

              My blog | My articles | MoXAML PowerToys

              R Offline
              R Offline
              RugbyLeague
              wrote on last edited by
              #7

              Using the Ants profiler it is (adding 1600 objects) roughly 3 times faster

              P 1 Reply Last reply
              0
              • P Pete OHanlon

                It's not a great idea to attempt to add bulk items into an ObservableCollection. That's not what it's designed to do - and will go slow because it raises a CollectionChanged event on every add. One option would be to derive a custom observable collection which suppresses this event until you have finished adding the data. Off the top of my head, this should work:

                public class SuperObservableCollection<T> : ObservableCollection<T>
                {
                private bool _suppressNotification = false;

                protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
                {
                if (!_suppressNotification)
                base.OnCollectionChanged(e);
                }

                public void AddRange(IEnumerable<T> list)
                {
                _suppressNotification = true;

                foreach (T item in list)
                {
                  Add(item);
                }
                \_suppressNotification = false;
                OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
                

                }
                }

                Deja View - the feeling that you've seen this post before.

                My blog | My articles | MoXAML PowerToys

                3 Offline
                3 Offline
                3fonov
                wrote on last edited by
                #8

                This is exactly what i'm looking for. Tried to do this before but doesnot take in mind about OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); Thanks a lot.

                P 1 Reply Last reply
                0
                • 3 3fonov

                  This is exactly what i'm looking for. Tried to do this before but doesnot take in mind about OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); Thanks a lot.

                  P Offline
                  P Offline
                  Pete OHanlon
                  wrote on last edited by
                  #9

                  You're welcome, and I'm glad I could help out.

                  Deja View - the feeling that you've seen this post before.

                  My blog | My articles | MoXAML PowerToys

                  1 Reply Last reply
                  0
                  • R RugbyLeague

                    Using the Ants profiler it is (adding 1600 objects) roughly 3 times faster

                    P Offline
                    P Offline
                    Pete OHanlon
                    wrote on last edited by
                    #10

                    I knew it would be faster, but that's quite a shocking amount.

                    Deja View - the feeling that you've seen this post before.

                    My blog | My articles | MoXAML PowerToys

                    R 1 Reply Last reply
                    0
                    • P Pete OHanlon

                      I knew it would be faster, but that's quite a shocking amount.

                      Deja View - the feeling that you've seen this post before.

                      My blog | My articles | MoXAML PowerToys

                      R Offline
                      R Offline
                      RugbyLeague
                      wrote on last edited by
                      #11

                      It was only a spot test - I ran it once using Add then again using your changes with AddRange - if it scales then it's a huge improvement but a few more profile runs would be needed to come to any conclusions. Good effort though :-D

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups