Returning IEnumerable vs. IQueryable

前端 未结 14 2868
梦毁少年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-21 23:47

    We can use both for the same way, and they are only different in the performance.

    IQueryable only executes against the database in an efficient way. It means that it creates an entire select query and only gets the related records.

    For example, we want to take the top 10 customers whose name start with ‘Nimal’. In this case the select query will be generated as select top 10 * from Customer where name like ‘Nimal%’.

    But if we used IEnumerable, the query would be like select * from Customer where name like ‘Nimal%’ and the top ten will be filtered at the C# coding level (it gets all the customer records from the database and passes them into C#).

提交回复
热议问题