简体   繁体   中英

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


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....

I think to get any kind of reliable result you would have to modify your test slightly.

Maybe you could start the Stopwatch, loop a 1000 times calling your fill method, stop the Stopwatch and then simply divide your result by 1000 to get an average.

Results can change drastically for a number of reasons (eg other processes and/or threads eating up CPU or the .NET Garbage Collection running). Peforming a test multiple times and taking an average will help smoothe out any discrepancies.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM