Join two collections with no extra memory RRS feed

  • Question

  • I apologize if this is not the right forum for a beginner question like this one. 


    I have 2 collections in different formats. One is:



    Code Snippet
    <XmlDataProvider x:Key="menuitemheaders" XPath="Items/Item">
            <Items xmlns="">
                      <Name>Item 1</Name>
                      <Name>Item 2</Name>
                      <Name>Item 3</Name>




    another is comma-separated string like:


    "Item 1,Item 3"  // note that Item 2 is missing


    I would like to join these two into a target collection which, if it were implemented in C# code using say ArrayList, would look like this:



    { string Name="Item 1", bool On=true; },

    { string Name="Item 2", bool On=false; }, // On is false because Item 2 is missing from comma sep. string

    { string Name="Item 3", bool On=true; }, 



    However, I don't really want to create new objects and copy field values into this join target collection object. Instead, I would like to create some 'lighter' object that would, for all intents and purposes (i.e. iteration, modification of field values but no insertion or deletion of collection elements), behave like this collection.


    If "Item 1" bool is set to false then comma-separated string would become just "Item 3". Since it has no collection interface, this comma-separated string is where, I guess, yield keyword would be useful to gain iteration.


    The intent is to bind UI to the boolean property On without actually implementing it as the C# property consuming storage.


    I think I know how to accomplish this in 'usual' C# code but I wonder if LINQ and extension methods can make for a more effecient and elegant approach. 

    Thursday, March 6, 2008 8:46 AM

All replies

  • It sounds like you are trying to do something that can only be done with custom logic.  However, I don't think you can accomplish the goal of not using any extra memory.  Even an object that auto-computes its values still takes up memory.


    Friday, March 7, 2008 3:40 AM
  • I agree. The choice of the title for the post was not good.

    That is why I mentioned a 'lighter' (memory-consumption-wise) object in the sense that it wouldn't be a duplicate of source collections.


    Friday, March 7, 2008 7:26 AM
  • OK, let me approach this from a different angle. Assuming that new collection would be created (and memory consumed) what would be LINQ query that would produce desired join result collection?

    What would be a stored procedure that could calculate new boolean On property?


    Hopefully this kind of question makes more sense.

    Friday, March 7, 2008 9:17 PM