A few days ago I posted some tests comparing ADO.Net and Linq to SQL. As expected Linq to SQL is worst that ADO.Net, but as could be seen in some tests the values were very approximated.
In one of the post I had received a comment saying that I should do this:
IEnumerable<PerformanceTestTable> resultQuery = (from p in db.PerformanceTestTables
select p).ToList();
instead of this:
IQueryable<PerformanceTestTable> resultQuery = from p in db.PerformanceTestTables
select p;
and that "Using ToList (or any of the four extensions which call GetEnumerator), will force LINQ to get all the records at once. You should see a performance gain after adding ToList()."
I had done the tests and most are best using ToList(). This was the results:
Direct Single Select
previous test results
| ADO.Net | ADO.Net | Linq to SQL | Linq to SQL |
| AVERAGE | MEDIAN | AVERAGE | MEDIAN |
| 469,8076 | 466 | 2747,09 | 2710 |
ToList() test results
| ADO.Net | ADO.Net | Linq to SQL | Linq to SQL |
| AVERAGE | MEDIAN | AVERAGE | MEDIAN |
| 469,8076 | 466 | 2759,952 | 2719 |
Direct Select All
previous test results
| ADO.Net | ADO.Net | Linq to SQL | Linq to SQL |
| AVERAGE | MEDIAN | AVERAGE | MEDIAN |
| 2788,928 | 2733 | 5476,776 | 5494 |
ToList() test results
| ADO.Net | ADO.Net | Linq to SQL | Linq to SQL |
| AVERAGE | MEDIAN | AVERAGE | MEDIAN |
| 2788,928 | 2733 | 5250,138 | 5112 |
Direct Select All
previous test results
| ADO.Net | ADO.Net | Linq to SQL | Linq to SQL |
| AVERAGE | MEDIAN | AVERAGE | MEDIAN |
| 964660,4 | 960886 | 1719343 | 1716473 |
ToList() test results
| ADO.Net | ADO.Net | Linq to SQL | Linq to SQL |
| AVERAGE | MEDIAN | AVERAGE | MEDIAN |
| 964660,4 | 960886 | 1710777 | 1713680 |
Another test that I had done lately in order to understand why I get such different results using Linq to SQL I was able to see that the object DataContext takes lot's of ticks just to be initialized.
| AVERAGE | MEDIAN |
| 147,1824 | 137 |
One other conclusion that I had take from these tests was that if I want to get similar result, between ADO.Net and Linq to SQL, I had to use Store Procedures.