Returning IEnumerable vs. IQueryable

前端 未结 14 2487
梦毁少年i
梦毁少年i 2020-11-21 22:59

What is the difference between returning IQueryable vs. IEnumerable, when should one be preferred over the other?



        
14条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 00:03

    Yes, both use deferred execution. Let's illustrate the difference using the SQL Server profiler....

    When we run the following code:

    MarketDevEntities db = new MarketDevEntities();
    
    IEnumerable first = db.WebLogs;
    var second = first.Where(c => c.DurationSeconds > 10);
    var third = second.Where(c => c.WebLogID > 100);
    var result = third.Where(c => c.EmailAddress.Length > 11);
    
    Console.Write(result.First().UserName);
    

    In SQL Server profiler we find a command equal to:

    "SELECT * FROM [dbo].[WebLog]"
    

    It approximately takes 90 seconds to run that block of code against a WebLog table which has 1 million records.

    So, all table records are loaded into memory as objects, and then with each .Where() it will be another filter in memory against these objects.

    When we use IQueryable instead of IEnumerable in the above example (second line):

    In SQL Server profiler we find a command equal to:

    "SELECT TOP 1 * FROM [dbo].[WebLog] WHERE [DurationSeconds] > 10 AND [WebLogID] > 100 AND LEN([EmailAddress]) > 11"
    

    It approximately takes four seconds to run this block of code using IQueryable.

    IQueryable has a property called Expression which stores a tree expression which starts being created when we used the result in our example (which is called deferred execution), and at the end this expression will be converted to an SQL query to run on the database engine.

提交回复
热议问题