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. Other Discussions
  3. Clever Code
  4. Evil Enumerations

Evil Enumerations

Scheduled Pinned Locked Moved Clever Code
csharpdatabaselinqcomdata-structures
5 Posts 4 Posters 15 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.
  • I Offline
    I Offline
    Ian Shlasko
    wrote on last edited by
    #1

    Someone in a previous thread asked me why I was casting a LINQ enumerable into an array before using it...

    public RecordList(IEnumerable<RecordView> en)
    : base(en)
    {
    _idLookup.Clear();
    int id;
    foreach (RecordView rv in en)
    if ((id = rv.BestID) > 0 && !_idLookup.ContainsKey(id))
    _idLookup.Add(id, rv);

    }

    (RecordList inherits from an ObservableCollection)

    Model.PositionCollection = new RecordList(
    DBInterface.Database.Provider.GetAllPositions().Select(a => new RecordView(a)));

    Looks nice and clean, right? WRONG. This was driving me so crazy that I started generating unique GUIDs for the RecordView objects to figure out why, for some reason, the RecordView objects being returned through the lookup (uses _idLookup) weren't the same ones that were actually IN the RecordList, and why updating one returned from the lookup wasn't passing change events to the view model... In fact, because of the way this RecordList is instantiated (Using a LINQ Select), _idLookup will NEVER contain the same objects that are in the collection itself, because that little Select() actually iterates twice... Once to run through the constructor, and then again to drop to the base constructor and actually populate the collection, yielding two distinct sets of objects... *grumbles and changes the constructor parameter to an array to make sure this NEVER happens again*

    Proud to have finally moved to the A-Ark. Which one are you in?
    Author of the Guardians Saga (Sci-Fi/Fantasy novels)

    P R 2 Replies Last reply
    0
    • I Ian Shlasko

      Someone in a previous thread asked me why I was casting a LINQ enumerable into an array before using it...

      public RecordList(IEnumerable<RecordView> en)
      : base(en)
      {
      _idLookup.Clear();
      int id;
      foreach (RecordView rv in en)
      if ((id = rv.BestID) > 0 && !_idLookup.ContainsKey(id))
      _idLookup.Add(id, rv);

      }

      (RecordList inherits from an ObservableCollection)

      Model.PositionCollection = new RecordList(
      DBInterface.Database.Provider.GetAllPositions().Select(a => new RecordView(a)));

      Looks nice and clean, right? WRONG. This was driving me so crazy that I started generating unique GUIDs for the RecordView objects to figure out why, for some reason, the RecordView objects being returned through the lookup (uses _idLookup) weren't the same ones that were actually IN the RecordList, and why updating one returned from the lookup wasn't passing change events to the view model... In fact, because of the way this RecordList is instantiated (Using a LINQ Select), _idLookup will NEVER contain the same objects that are in the collection itself, because that little Select() actually iterates twice... Once to run through the constructor, and then again to drop to the base constructor and actually populate the collection, yielding two distinct sets of objects... *grumbles and changes the constructor parameter to an array to make sure this NEVER happens again*

      Proud to have finally moved to the A-Ark. Which one are you in?
      Author of the Guardians Saga (Sci-Fi/Fantasy novels)

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      ... ooorrr... don't use Linq... :~

      J 1 Reply Last reply
      0
      • I Ian Shlasko

        Someone in a previous thread asked me why I was casting a LINQ enumerable into an array before using it...

        public RecordList(IEnumerable<RecordView> en)
        : base(en)
        {
        _idLookup.Clear();
        int id;
        foreach (RecordView rv in en)
        if ((id = rv.BestID) > 0 && !_idLookup.ContainsKey(id))
        _idLookup.Add(id, rv);

        }

        (RecordList inherits from an ObservableCollection)

        Model.PositionCollection = new RecordList(
        DBInterface.Database.Provider.GetAllPositions().Select(a => new RecordView(a)));

        Looks nice and clean, right? WRONG. This was driving me so crazy that I started generating unique GUIDs for the RecordView objects to figure out why, for some reason, the RecordView objects being returned through the lookup (uses _idLookup) weren't the same ones that were actually IN the RecordList, and why updating one returned from the lookup wasn't passing change events to the view model... In fact, because of the way this RecordList is instantiated (Using a LINQ Select), _idLookup will NEVER contain the same objects that are in the collection itself, because that little Select() actually iterates twice... Once to run through the constructor, and then again to drop to the base constructor and actually populate the collection, yielding two distinct sets of objects... *grumbles and changes the constructor parameter to an array to make sure this NEVER happens again*

        Proud to have finally moved to the A-Ark. Which one are you in?
        Author of the Guardians Saga (Sci-Fi/Fantasy novels)

        R Offline
        R Offline
        Robert Rohde
        wrote on last edited by
        #3

        Calling ToArray will result in doubled memory usage. How about changing your constructor like this:

        public RecordList(IEnumerable<RecordView> en)
        : base(en)
        {
        _idLookup.Clear();
        int id;
        foreach (RecordView rv in this) //<- change is here
        if ((id = rv.BestID) > 0 && !_idLookup.ContainsKey(id))
        _idLookup.Add(id, rv);

        }

        Why this works? Because the base constructor copies all elements into his internal list. Robert

        I 1 Reply Last reply
        0
        • R Robert Rohde

          Calling ToArray will result in doubled memory usage. How about changing your constructor like this:

          public RecordList(IEnumerable<RecordView> en)
          : base(en)
          {
          _idLookup.Clear();
          int id;
          foreach (RecordView rv in this) //<- change is here
          if ((id = rv.BestID) > 0 && !_idLookup.ContainsKey(id))
          _idLookup.Add(id, rv);

          }

          Why this works? Because the base constructor copies all elements into his internal list. Robert

          I Offline
          I Offline
          Ian Shlasko
          wrote on last edited by
          #4

          Hmm... Right, the collection would get filled up before the derived constructor gets called... Why didn't I think of that? Nice fix... Thanks.

          Proud to have finally moved to the A-Ark. Which one are you in?
          Author of the Guardians Saga (Sci-Fi/Fantasy novels)

          1 Reply Last reply
          0
          • P PIEBALDconsult

            ... ooorrr... don't use Linq... :~

            J Offline
            J Offline
            J4amieC
            wrote on last edited by
            #5

            What a weird response.

            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