Returning IEnumerable vs. IQueryable

前端 未结 14 2735
梦毁少年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:02

    In general terms I would recommend the following:

    • Return IQueryable if you want to enable the developer using your method to refine the query you return before executing.

    • Return IEnumerable if you want to transport a set of Objects to enumerate over.

    Imagine an IQueryable as that what it is - a "query" for data (which you can refine if you want to). An IEnumerable is a set of objects (which has already been received or was created) over which you can enumerate.

提交回复
热议问题