pondelok 6. februára 2012

List of objects to Dictionary

Example of using extension method  ToDictionary.
Maybe you can found this my code  useful.

The first is converting list to dictionary with key-selector.
The second extracts only two atributes (key, val)

Code Snippet
  1. class People
  2.     {
  3.         public string userID { get; set; }
  4.         public string name { get; set; }
  5.         public string phone { get; set; }
  6.         public string email { get; set; }
  7.     }
  8.         
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.            List<People> users = new List<People>();
  14.  
  15.            //p.Add()...
  16.  
  17.  
  18.            Dictionary<string, People> temp =
  19.                               users.ToDictionary(c => c.userID);
  20.  
  21.            Dictionary<string, string> eMails =
  22.                         users.ToDictionary(c => c.userID, c => c.email);
  23.          
  24.         }
  25.     }

Žiadne komentáre:

Zverejnenie komentára