2011-03-13 34 views
2

嗨 我想使用通過使用NHibenrate讀取數據庫並具有1000條記錄而獲得的數據傳輸對象,並且我需要謹慎使用此DTo,其中基於某個鍵我選擇一個值。我的DTO會像這樣變成東西。帶有O(1)查找的數據傳輸對象

public class DTO 
{ 
    string name; 
    int id; 
    int schoolId; 
    double value; 
} 

現在的問題是,我得到這個作爲一個枚舉。現在基於該schoolid ID和姓名,我需要選擇一個值,我做截至目前,因爲這可枚舉如下:

DtoList.Where(x=>x.name="name" && x.id=1 && x.schoolId=2).First(); 

現在的問題是這是一個O(n)查找,我希望它是O(1)可以通過使用IDictionary來完成。

我想知道如果我可以讓這個DTO實現IDitcionary,然後做同樣的事情。 \

這可能嗎?我認爲這更多來自c#的角度。

同樣從NHibernate的角度來看這將如何解決。

+0

將這項工作爲你 ? http://ayende.com/blog/4548/nhibernate-streaming-large-result-sets – 2015-05-11 17:13:27

回答

0

如果你曾經構建列表,然後多次查找它,也許這是值得創建另一個對象,其中將包括一個字典,並從DTO的集合創建它:

struct Key 
{ 
    string Name; 
    int Id; 
    int SchoolId; 
} 

class DictionaryObject 
{ 
    IDictionary<Key, double> _dict; 

    public DictionaryObject(IEnumerable<Dto> dtoList) 
    { 
     _dict = dtoList.ToDictionary(
      o => new Key { Name = o.Name, Id = o.Id, SchoolId = o.SchoolId }, 
      o => o.Value); 
    } 

    double GetValue(string Name, int Id, int SchoolId) 
    { 
     // if the key exists then... 
     return _dict[new Key { Name = Name, Id = Id, SchoolId = SchoolId }]; 
    } 

    ... 
} 
+0

我如何轉換它給出的例子提到..我如何生成密鑰? – Mary 2011-03-13 17:32:30

+0

@瑪麗,我編輯了答案,你覺得現在更清楚了嗎? – 2011-03-13 17:38:32

+0

我想知道做這個關鍵的收益是多少。同時使用這本字典我需要建立一個密鑰,再次nt最佳。 – Mary 2011-03-13 17:40:44