none
Dictionary pointing to ObservableCollection? RRS feed

  • Question

  • I have an ObservableCollection<Employee>

    where Employee (Id, FirstName, MiddleName, LastName, Age, etc....)

    I have a function GetAge(FirstName, MiddleName, LastName) which returns the corresponding Age. Let's assume, combo of these three values in unique.

    Now, lets say, this collection has 1 Million items in it and the function GetAge() is called by multiple classes and several times. Note that I can not have GetAge(Id) is my actual app, though it is a unique ID of type int.

    My options:

    1. Use a foreach loop to iterate and locate a match or use LINQ query (Not preferred).

    2. I Save the data to a nested dictionary Dictionary<FirstName, Dictionary<MiddleName, Dictionary<LastName, Age >>> for faster lookup. This is what I have done. However, if an Age in the underlying class changes, this dictionary too needs to be updated.

    How do I make this a reference dictionary to original class items? Or any other better approach?

    Thanks.

    Monday, November 13, 2017 3:57 PM

All replies

  • Hi Jay,

    Change the LastName dictionary to Employee instead:

    Dictionary<FirstName, Dictionary<MiddleName, Dictionary<LastName, Employee >>>

    You can then, obviously, get the Age from the Employee.


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Monday, November 13, 2017 4:38 PM
    Moderator
  • Yes, but when I populate this dictionary, wouldn't Employee be a copy of the Employee in the ObservableCollection and not a reference to the specific Employee object in the original ObservableCollection? 

    Monday, November 13, 2017 4:59 PM
  • I doubt if it would be a copy, it should be a reference to the specific Employee object in the collection ...  unless when you populate the Dictionary, you specifically use a Copy. What does that piece of code look like (for populating the Dictionary)?

    BTW, why don't you want to use a LINQ query to begin with ... it seems a little crazy to have 1 million records in an ObservableCollection and then have a million in a Dictionary too.


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Monday, November 13, 2017 5:16 PM
    Moderator
  • Hello ,

    I really don't suggest you load over 1 million records to memory and this is a big disaster for the computer. Sql server technology is a good choice for you. Why not store the value into database and  build search statement like "select * from db where Firstname='xxx' and MiddleName='xxx' and LastName='xxx' ".This is an elegant and professional approach.

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 15, 2017 10:09 AM
    Moderator
  • Hello JayShah203,

    Did you have anything to update?  If you already have got the useful solution from there, you could close the thread by marking the useful reply  as answer, This can be beneficial to other community members reading this thread. Thanks a lot.

    Sincerely,
    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, November 26, 2017 11:57 AM
    Moderator