2010-08-25 67 views
1

部分:firstorDefault業績不斷攀升的代碼

Dictionary<Calculation, List<PropertyValue>> result = new Dictionary<Calculation, List<PropertyValue>>(); 
while (reader != null && reader.Read()) //it loops about 60000, and it will be bigger 
{ 
    #region create calc and propvalue variables 
    //... 
    #endregion 

    //this FirstOrDefault needs a lot of time 
    tmpElementOfResult = result.Keys.FirstOrDefault(r => r.InnerID == calc.InnerID); 
    if (tmpElementOfResult == null) 
    { 
     result.Add(calc, new List<PropertyValue> { propValue }); 
    } 
    else 
    { 
     result[tmpElementOfResult].Add(propValue); 
    } 
} 

你能給我一些想法如何使它更快,因爲現在它的大約25秒:(

+0

把讀卡器= NULL比較 - 雖然條款之外。現在你正在檢查它約60000 ...當你可以檢查一次。 – Jonathan 2010-08-25 10:09:09

+0

是的,我修復它,這是一個錯誤:)。謝謝! – 2010-08-25 11:16:16

回答

2

這聽起來像你應該有一個?從calc.InnerID類型的字典,而不是一個Dictionary<Calc, ...>。這樣,你可以更快速地執行查詢。你確實需​​要存儲Calc本身所有,還是你只是感興趣的ID?

例如:

Dictionary<Guid, List<PropertyValue>> result = 
    new Dictionary<Guid, List<PropertyValue>>(); 
while (reader.Read()) 
{ 
    // Work out calc 
    List<PropertyValue> list; 
    if (!result.TryGetValue(calc.InnerID, out list)) 
    { 
     list = new List<PropertyValue>(); 
     result[calc.InnerID] = list; 
    } 
    list.Add(propValue); 
} 

另外,如果你可以在讀寫轉換爲IEnumerable<Calc>你可以使用:

Lookup<Guid, PropertyValue> result = items.ToLookup(x => x.InnerID, 
                // Or however you get it... 
                x => x.PropertyValue); 

編輯:這聽起來像是兩個計算器值應被視爲平等的,如果他們有相同的InnerID,對不對?因此,在Calc內覆蓋EqualsGetHashCode以參考InnerID。然後,你可以使用:

Lookup<Calc, PropertyValue> result = items.ToLookup(x => x, 
                // Or however you get it... 
                x => x.PropertyValue); 

...或者你可以使用類似的代碼的第一個片段,但有Dictionary<Calc, ...>:的

Dictionary<Calc, List<PropertyValue>> result = 
    new Dictionary<Calc, List<PropertyValue>>(); 
while (reader.Read()) 
{ 
    // Work out calc 
    List<PropertyValue> list; 
    if (!result.TryGetValue(calc, out list)) 
    { 
     list = new List<PropertyValue>(); 
     result[calc] = list; 
    } 
    list.Add(propValue); 
} 
+0

是的,只存儲innerID的速度要快得多,但是我需要以後的整個calc :(。 感謝您的回答,它非常有用!您能否給我的新答案一下看看? – 2010-08-25 11:24:50

+0

@Zoltan:編輯。 – 2010-08-25 11:59:59

+0

感謝答案! 最後,我解決了它不使用整個calc,所以我使用你的答案的第一個版本:)。 如果我只是覆蓋平等,並且再次使用整個calc,這不會很慢嗎?是不是像我的原來幾乎相同的解決方案: result.Keys.FirstOrDefault(r => r.InnerID == calc.InnerID); ? – 2010-08-25 12:56:47

0

代替

tmpElementOfResult = result.Keys.FirstOrDefault(r => r.InnerID == calc.InnerID); 

使用

result.ContainsKey(calc.InnerId); 

檢查是否鑰匙存在。

+0

如果不改變'result'的類型就不會起作用。 – 2010-08-25 10:09:02

0

是否有可能做這樣的事情:

   lookUpForResult = result.ToLookup(x => x.Key.InnerID, x => x.Value); 

       if (lookUpForResult.Contains(calc.InnerID)) 
       { 
        result.Add(calc, new List<PropertyValue> { propValue }); 
       } 
       else 
       { 
        (lookUpForResult[calc.InnerID]).Add(propValue); 
       } 
+0

查找的關鍵是它爲你做了所有這些。你不需要自己添加值。這也應該是對你的問題的編輯......它不是你的問題的答案。 – 2010-08-25 11:57:10

+0

我無法從LookUp :()中找到List 。 – 2010-08-25 12:10:43

+0

我解決了它不使用整個計算。感謝您的答案! – 2010-08-25 12:21:58