我填充2的dataGridView二種方法的:
1)Lambda表達式:
哪一個更適合填充DataGridView lambda或linq?
protected void FillLamdaMethod()
{
Stopwatch sw = Stopwatch.StartNew();
using (eCommerceContext ctx = new eCommerceContext())
{
List<table_bilgisayar> listBilgisayar = new List<table_bilgisayar>();
listBilgisayar = ctx.table_bilgisayar.ToList();
dataGridViewLamda.DataSource = listBilgisayar;//qry.AsEnumerable();
}
sw.Stop();
lblLamdaResult.Text = String.Format("Time used (float): {0} ms",sw.Elapsed.TotalMilliseconds)+Environment.NewLine;
lblLamdaResult.Text+=String.Format("Time used (rounded): {0} ms", sw.ElapsedMilliseconds);
}
2)的LINQ方法:
protected void FillClassicMethod()
{
Stopwatch sw = Stopwatch.StartNew();
using (eCommerceContext ctx = new eCommerceContext())
{
List<table_bilgisayar> listBilgisayar = new List<table_bilgisayar>();
listBilgisayar =(from q in ctx.table_bilgisayar select q).ToList();
dataGridViewClasicLinq.DataSource = listBilgisayar;//(from q in ctx.table_bilgisayar select q.model).ToList();
}
sw.Stop();
lblClassicResult.Text = String.Format("Time used (float): {0} ms", sw.Elapsed.TotalMilliseconds)+Environment.NewLine;
lblClassicResult.Text += String.Format("Time used (rounded): {0} ms", sw.ElapsedMilliseconds);
}
我有2個重要問題
1)這種秒錶方法是正確還是充分,還是有更好的方法來計算性能?
2)這次;我知道,lambda表達式是(在表...來自x),但是測試結果比經典LINQ快多了是令人驚訝:
1)LAMBDA方法:867毫秒
2)Linq的方法:39毫秒 這結果是正確的?我預計它必須恰好相反...
也請單擊fillButton來調用此方法。表現結果愚蠢地改變。我認爲這很瘋狂。 867毫秒秒點擊結果56毫秒第三點擊45毫秒....
您是否正在運行這些?你的結果是否被JIT歪曲?您的數據源是否第二次從緩存中提供結果? – 2010-12-09 21:58:14