Search code examples
c#lambdalinq-to-objectsperformancecounterstopwatch

which one is better for filling DataGridView lambda or linq?


i filled 2 dataGridView with two kinds of method:
1) Lambda Expression:

  
 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 Method:

 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);
        }


i have 2 important question
1) this stopwatch method is correct or sufficient or is there ant better method to compute performance?
2) Up this time; i know that lambda expression is MORE FASTER than classic linq ( from x in table etc...) But Test result is surprising:
1) Lambda Method : 867 ms
2) Linq Method: 39 ms This result is correct? i expect it must be just the opposite...

ALSO click fillButton to call this methods. performance result stupidly change. i think that this is crazy. 867 ms second click result 56 ms third click 45 ms....


Solution

  • I am surprised there is any difference, i thought it's just a different syntax. Maybe your Select (q=>q) slows it down. Try

    listBilgisayar = ctx.table_bilgisayar.ToList();