Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 27th, 2012  |  syntax: None  |  size: 0.70 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Data transfer object with O(1) lookup
  2. public class DTO
  3. {
  4.     string name;
  5.     int id;
  6.     int schoolId;
  7.     double value;
  8. }
  9.        
  10. DtoList.Where(x=>x.name="name" && x.id=1 && x.schoolId=2).First();
  11.        
  12. struct Key
  13. {
  14.     string Name;
  15.     int Id;
  16.     int SchoolId;
  17. }
  18.  
  19. class DictionaryObject
  20. {
  21.    IDictionary<Key, double> _dict;
  22.  
  23.    public DictionaryObject(IEnumerable<Dto> dtoList)
  24.    {
  25.       _dict = dtoList.ToDictionary(
  26.              o => new Key { Name = o.Name, Id = o.Id, SchoolId = o.SchoolId },
  27.              o => o.Value);
  28.    }
  29.  
  30.    double GetValue(string Name, int Id, int SchoolId)
  31.    {
  32.       // if the key exists then...
  33.       return _dict[new Key { Name = Name, Id = Id, SchoolId = SchoolId }];
  34.    }
  35.  
  36.    ...
  37. }