假設我們有一些共同的特性,這些業務對象:性能問題迭代在C#泛型列表時
public class A
{
// Properties in common
public int Common { get; set; }
public string aValue { get; set; }
// Some other goes here.
}
public class B
{
// Properties in common
public int Common { get; set; }
public string bValue { get; set; }
// Some other goes here.
}
而且在我們的業務邏輯,我們有兩個列表像這樣的:
List<A> aList = new List<A>();
List<B> bList = new List<B>();
(並且假設我們有這些列表,每個列表至少填充100個實例) 好吧,讓我們從我們的問題開始,我們需要遍歷aList以設置bList中每個實例的一個屬性,當然匹配與財產共同的,像th是:
foreach (A a in aList)
{
B b = bList.Find(x => x.Common == a.Common);
if (b != null)
b.bValue = a.aValue;
}
有誰知道一個更好的方法來改善這種操作,因爲它是導致我們的應用程序需要太多的時間來完成?
感謝,
我不認爲100元可引起任何性能問題。那裏有多少? – gdoron 2012-04-18 00:39:26
@gdoron這取決於你多久調用一次這個功能:) – dasblinkenlight 2012-04-18 00:43:57
@gdoron我想我忘了提到該應用是用於Windows Mobile設備的,所以該應用受到移動設備資源的限制。 – sammiiuu 2012-04-18 00:45:37