locked
How do I convert a Dictionary of objects into a List of objects RRS feed

  • Question

  • In a Windows Store (WinRT) app,  have a Dictionary of objects defined as:

    public static Dictionary SessionState = new Dictionary();

    I have added a number of items to SessionState with the key "Custom Demographics"

    I want to extract all the SessionState items with the key "Custom Demographics" and load them into a List of type Demographic (the type of the objects loaded into the Dictionary with the key of "Custom Demographics"

    I need to use a foreach loop to iterate through all the obects in SessionState with a key of "Custom Demographics." I figured putting them in a separate List would facilitate this, since I could not come up with a syntax that would work to iterate through the SessionState items.

    What is the proper syntax to do the conversion from Dictionary to List in this case?

    Thursday, April 18, 2013 11:59 PM

Answers

  • Try this -

    class Demographic
            {
                public string DemoGraphicName { get; set; }
                public Demographic(string demographic)
                {
                    DemoGraphicName = demographic;
                }
            }
    
            class NonDemographic
            {
                public string DemoGraphicName { get; set; }
                public NonDemographic(string demographic)
                {
                    DemoGraphicName = demographic;
                }
            }

       Dictionary<string, object> SessionState = new Dictionary<string, object>();
                   
                    List<Demographic> lstDemoGraphics = new List<Demographic>();
                    lstDemoGraphics.Add(new Demographic("Custom Demographics 1"));
                    lstDemoGraphics.Add(new Demographic("Custom Demographics 2"));
                    lstDemoGraphics.Add(new Demographic("Custom Demographics 3"));
                    SessionState.Add("Custom Demographics", lstDemoGraphics);
    
    
                    List<NonDemographic> lstNonDemoGraphics = new List<NonDemographic>();
                    lstNonDemoGraphics.Add(new NonDemographic("Custom Demographics 1"));
                    lstNonDemoGraphics.Add(new NonDemographic("Custom Demographics 2"));
                    lstNonDemoGraphics.Add(new NonDemographic("Custom Demographics 3"));
                    SessionState.Add("Non Demographics", lstNonDemoGraphics);
    
                    if (SessionState.ContainsKey("Custom Demographics"))
                    {
                        List<Demographic> lstDemos = SessionState["Custom Demographics"] as List<Demographic>;
                    }
    
                    if (SessionState.ContainsKey("Non Demographics"))
                    {
                        List<NonDemographic> lstNonDemos = SessionState["Non Demographics"] as List<NonDemographic>;
                    }


    Thanks,
    Sachin
    My Samples | Personal Website

    Friday, April 19, 2013 1:53 AM

All replies

  • Not sure if I understood your question completely. Is something like this you are looking for?

     Dictionary<string, List<string>> SessionState = new Dictionary<string, List<string>>();
                    List<String> lstDemoGraphics = new List<string>();
                    lstDemoGraphics.Add("Custom Demographics 1");
                    lstDemoGraphics.Add("Custom Demographics 2");
                    lstDemoGraphics.Add("Custom Demographics 3");
                    SessionState.Add("Custom Demographics", lstDemoGraphics); // Serializing here
    
                    if (SessionState.ContainsKey("Custom Demographics"))
                    {
                        List<string> lstDemos = SessionState["Custom Demographics"]; // Retrieving here
                    }

    In above code you can replace the string to any type (List<string> -> List<Demographic>) and then add object (stDemoGraphics.Add("Custom Demographics 1") -> lstDemoGraphics.Add(new Demographic ()) ) of that type instead of string.


    Thanks,
    Sachin

    My Samples |Personal Website




    • Edited by Sachin S Friday, April 19, 2013 12:12 AM
    Friday, April 19, 2013 12:06 AM
  • Tried the following, but got the error: "Cannot implicitly convert type 'NonProfitDemographics.Data.Demographic' to 'System.Collections.Generic.List<NonProfitDemographics.Data.Demographic>'"

    List<Demographic> customDemographics = (Demographic)App.SessionState.Keys.Select(k => k.Equals("Custom Demographics"));

    Friday, April 19, 2013 12:14 AM
  • Try this 

    List<Demographic> customDemographics = App.SessionState["Custom Demographics"]; // Assuming SessionState is dictionary of key string and object of type Demographic


    Thanks,
    Sachin
    My Samples | Personal Website


    • Edited by Sachin S Friday, April 19, 2013 1:22 AM
    Friday, April 19, 2013 1:18 AM
  • SessionState is just a Dictionary of objects, and that is where the problem comes in. The objects I'm interested in, those having a key of "Custom Demogrpahics", happen to be objects of type Demographic, and errors come up when I try to retrieve them and convert to a List of Demographic objects.
    Friday, April 19, 2013 1:40 AM
  • Here is example - 

    class Demographic
            {
                public string DemoGraphicName { get; set; }
                public Demographic(string demographic)
                {
                    DemoGraphicName = demographic;
                }
            }

      Dictionary<string, List<Demographic>> SessionState = new Dictionary<string, List<Demographic>>();
                    List<Demographic> lstDemoGraphics = new List<Demographic>();
                    lstDemoGraphics.Add(new Demographic("Custom Demographics 1"));
                    lstDemoGraphics.Add(new Demographic("Custom Demographics 2"));
                    lstDemoGraphics.Add(new Demographic("Custom Demographics 3"));
                    SessionState.Add("Custom Demographics", lstDemoGraphics);
    
                    if (SessionState.ContainsKey("Custom Demographics"))
                    {
                        List<Demographic> lstDemos = SessionState["Custom Demographics"];
                    }


    Thanks,
    Sachin
    My Samples | Personal Website

    Friday, April 19, 2013 1:42 AM
  • You're close, but the list I want is a List<Demographic> rather than a List<string>. Not all the objects stored in the Dictionary are of type Demographic. Other items, with different keys (not "Custom Demographics") are other types of objects.
    Friday, April 19, 2013 1:42 AM
  • Yes if it is mix, can you assume that if key is Custom Demographics it will be of type - Demographic? 

    Else check for type of object, then deserialize accordingly. 


    Thanks,
    Sachin
    My Samples | Personal Website

    Friday, April 19, 2013 1:45 AM
  • Try this -

    class Demographic
            {
                public string DemoGraphicName { get; set; }
                public Demographic(string demographic)
                {
                    DemoGraphicName = demographic;
                }
            }
    
            class NonDemographic
            {
                public string DemoGraphicName { get; set; }
                public NonDemographic(string demographic)
                {
                    DemoGraphicName = demographic;
                }
            }

       Dictionary<string, object> SessionState = new Dictionary<string, object>();
                   
                    List<Demographic> lstDemoGraphics = new List<Demographic>();
                    lstDemoGraphics.Add(new Demographic("Custom Demographics 1"));
                    lstDemoGraphics.Add(new Demographic("Custom Demographics 2"));
                    lstDemoGraphics.Add(new Demographic("Custom Demographics 3"));
                    SessionState.Add("Custom Demographics", lstDemoGraphics);
    
    
                    List<NonDemographic> lstNonDemoGraphics = new List<NonDemographic>();
                    lstNonDemoGraphics.Add(new NonDemographic("Custom Demographics 1"));
                    lstNonDemoGraphics.Add(new NonDemographic("Custom Demographics 2"));
                    lstNonDemoGraphics.Add(new NonDemographic("Custom Demographics 3"));
                    SessionState.Add("Non Demographics", lstNonDemoGraphics);
    
                    if (SessionState.ContainsKey("Custom Demographics"))
                    {
                        List<Demographic> lstDemos = SessionState["Custom Demographics"] as List<Demographic>;
                    }
    
                    if (SessionState.ContainsKey("Non Demographics"))
                    {
                        List<NonDemographic> lstNonDemos = SessionState["Non Demographics"] as List<NonDemographic>;
                    }


    Thanks,
    Sachin
    My Samples | Personal Website

    Friday, April 19, 2013 1:53 AM