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. C#
  4. ObservableCollection Constructor

ObservableCollection Constructor

Scheduled Pinned Locked Moved C#
csharpquestion
9 Posts 3 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.
  • M Offline
    M Offline
    Michael Sync
    wrote on last edited by
    #1

    I'm facing some problems with ObservableCollection constructor.. Any idea why this code is not working? entity.Currencies = new ObservableCollection(CurreniesList.Where( c => c.IsSelected)); Note: 1. entity.Currencies is ObservableCollection(). 2. CurrenciesList is ObservableCollection(). 3. CurrencyEntity class has a proprety called "IsSelected". Thanks in advance. Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)

    S L 2 Replies Last reply
    0
    • M Michael Sync

      I'm facing some problems with ObservableCollection constructor.. Any idea why this code is not working? entity.Currencies = new ObservableCollection(CurreniesList.Where( c => c.IsSelected)); Note: 1. entity.Currencies is ObservableCollection(). 2. CurrenciesList is ObservableCollection(). 3. CurrencyEntity class has a proprety called "IsSelected". Thanks in advance. Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      ObservableCollection < T > 's constructor takes a List < T >, and not IEnumerable < T >, presumably because only then can it "observe" addition and removal of elements. Simply wrapping the result of CurrenciesList.Where in a list should solve the problem.

      new ObservableCollection <CurrencyEntity>(
      new List<CurrencyEntity>(currenciesList.Where(c => c.IsSelected)));

      Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

      M 1 Reply Last reply
      0
      • S S Senthil Kumar

        ObservableCollection < T > 's constructor takes a List < T >, and not IEnumerable < T >, presumably because only then can it "observe" addition and removal of elements. Simply wrapping the result of CurrenciesList.Where in a list should solve the problem.

        new ObservableCollection <CurrencyEntity>(
        new List<CurrencyEntity>(currenciesList.Where(c => c.IsSelected)));

        Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

        M Offline
        M Offline
        Michael Sync
        wrote on last edited by
        #3

        Hi Senthil, Thanks but I'm still getting the error. There are 3 constructors[^] in ObservableCollection. IEnumerable[^] is one of them. Or, Am I missing something??

        Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)

        S 1 Reply Last reply
        0
        • M Michael Sync

          I'm facing some problems with ObservableCollection constructor.. Any idea why this code is not working? entity.Currencies = new ObservableCollection(CurreniesList.Where( c => c.IsSelected)); Note: 1. entity.Currencies is ObservableCollection(). 2. CurrenciesList is ObservableCollection(). 3. CurrencyEntity class has a proprety called "IsSelected". Thanks in advance. Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          This sounds a bit strange, because this is working for me:

          public class CurrencyEntity
          {
          public bool IsSelected
          {
          get { return true; }
          }
          }

          This is just a stub, and here's the code:

          List<CurrencyEntity> c1 = new List<CurrencyEntity>() { new CurrencyEntity() };
          ObservableCollection<CurrencyEntity> ob = new ObservableCollection<CurrencyEntity>(c1.Where(c => c.IsSelected));

          M 1 Reply Last reply
          0
          • M Michael Sync

            Hi Senthil, Thanks but I'm still getting the error. There are 3 constructors[^] in ObservableCollection. IEnumerable[^] is one of them. Or, Am I missing something??

            Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)

            S Offline
            S Offline
            S Senthil Kumar
            wrote on last edited by
            #5

            My bad, I missed out the third overload. Can you paste the error that you are getting? [EDIT] .NET 3.0 has only two overloads (http://msdn.microsoft.com/en-us/library/ms658737(VS.85).aspx[^])

            Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

            M 1 Reply Last reply
            0
            • S S Senthil Kumar

              My bad, I missed out the third overload. Can you paste the error that you are getting? [EDIT] .NET 3.0 has only two overloads (http://msdn.microsoft.com/en-us/library/ms658737(VS.85).aspx[^])

              Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

              M Offline
              M Offline
              Michael Sync
              wrote on last edited by
              #6

              The error message is like it doesn't a constructor that takes 1 argument. I'm using 3.5.... Am i referencing the wrong dll or need to import some namespaces? it's very strange

              Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)

              S 1 Reply Last reply
              0
              • L Lost User

                This sounds a bit strange, because this is working for me:

                public class CurrencyEntity
                {
                public bool IsSelected
                {
                get { return true; }
                }
                }

                This is just a stub, and here's the code:

                List<CurrencyEntity> c1 = new List<CurrencyEntity>() { new CurrencyEntity() };
                ObservableCollection<CurrencyEntity> ob = new ObservableCollection<CurrencyEntity>(c1.Where(c => c.IsSelected));

                M Offline
                M Offline
                Michael Sync
                wrote on last edited by
                #7

                I will try again and let you know. it's very strange..

                Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)

                M 1 Reply Last reply
                0
                • M Michael Sync

                  The error message is like it doesn't a constructor that takes 1 argument. I'm using 3.5.... Am i referencing the wrong dll or need to import some namespaces? it's very strange

                  Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)

                  S Offline
                  S Offline
                  S Senthil Kumar
                  wrote on last edited by
                  #8

                  What does hitting F12 (Go to Definition) on the ObservableCollection show?

                  Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

                  1 Reply Last reply
                  0
                  • M Michael Sync

                    I will try again and let you know. it's very strange..

                    Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)

                    M Offline
                    M Offline
                    Michael Sync
                    wrote on last edited by
                    #9

                    Thanks. I think it doesn't work because of Silverlight. I'm using Prism v2 (with multi-targeting feature). so, i need to create the class in SL project and link that class from wpf. that's why the code doesn't work. Thanks for your help.

                    Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)

                    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